npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ask-imon/time_diff_calc

v1.1.2

Published

A package that will help user calculate difference between 2 timestamps and get the output in multiple format, based on requirements.

Downloads

12

Readme

Timestamp Difference Calculator

This JavaScript utility calculates the difference between two given timestamps and returns the result in various formats.

Features

  • Calculate Time Difference: Easily calculate the time difference between two timestamps.
  • Multiple Formats: The system supports returning the time difference in various formats, including milliseconds, seconds, minutes, hours, days, weeks, months, years, a combined format of days, hours, minutes, and seconds (DHMS), and a detailed format specifying years, days, hours, minutes and seconds.

Installation

To use this utility in your project, simply include the JavaScript file in your project directory.

Usage

The function timeDiffCalc can be used as follows:

// Import the function and FormatTypes - assuming they are in the same file
const { timeDiffCalc, FormatTypes } = require('@ask-imon/time_diff_calc');

// Example timestamps
const timestamp1 = "2022-01-01T00:00:00Z";
const timestamp2 = "2023-01-02T12:30:45Z";

// Calculate the difference in a specific format

// To get the difference in Milliseconds
const differenceInMilliseconds = timeDiffCalc(timestamp1, timestamp2, FormatTypes.MILLISECOND);
console.log(`Difference in Milliseconds: ${differenceInMilliseconds}`);

// To get the difference in Seconds
const differenceInSecond = timeDiffCalc(timestamp1, timestamp2, FormatTypes.SECOND);
console.log(`Difference in Seconds: ${differenceInSecond}`);

// To get the difference in Minutes
const differenceInMinutes = timeDiffCalc(timestamp1, timestamp2, FormatTypes.MINUTE);
console.log(`Difference in Minutes: ${differenceInMinutes}`);

// To get the difference in Hours (Approx.)
const differenceInHours = timeDiffCalc(timestamp1, timestamp2, FormatTypes.HOUR);
console.log(`Difference in Hours: ${differenceInHours}`);

// To get the difference in Day (Approx.)
const differenceInDays = timeDiffCalc(timestamp1, timestamp2, FormatTypes.DAY);
console.log(`Difference in Days: ${differenceInDays}`);

// To get the difference in Weeks (Approx.)
const differenceInWeeks = timeDiffCalc(timestamp1, timestamp2, FormatTypes.WEEK);
console.log(`Difference in Weeks: ${differenceInWeeks}`);

// To get the difference in Months (Approx.)
const differenceInMonths = timeDiffCalc(timestamp1, timestamp2, FormatTypes.MONTH);
console.log(`Difference in Months: ${differenceInMonths}`);

// To get the difference in Years (Approx.)
const differenceInYears = timeDiffCalc(timestamp1, timestamp2, FormatTypes.YEAR);
console.log(`Difference in Years: ${differenceInYears}`);

// To get the difference in DHMS (Days, Hours, Minutes, Seconds)
const differenceInDHMS = timeDiffCalc(timestamp1, timestamp2, FormatTypes.DHMS);
console.log(`Difference in DHMS: ${differenceInDHMS}`);

// To get a detailed breakdown
const detailedDifference = timeDiffCalc(timestamp1, timestamp2, FormatTypes.DETAIL);
console.log(`Detailed difference: ${detailedDifference}`);

Outputs

Difference in Milliseconds: 31667445000
Difference in Seconds: 31667445
Difference in Minutes: 527790
Difference in Hours: 8796
Difference in Days: 366
Difference in Weeks: 52
Difference in Months: 12
Difference in Years: 1
Difference in DHMS: 366 Days, 12 Hours, 30 Minutes, 45 Seconds
Detailed difference: 1 Year, 1 Day, 12 Hours, 30 Minutes, 45 Seconds

Parameters

  • timestamp1 (string): First timestamp.
  • timestamp2 (string): Second timestamp.
  • format (Enum FormatTypes): The desired output format.

Returns

  • (number|string): The difference between the timestamps in the desired format.

Exceptions

  • Throws an error if an unsupported format is provided.

Contributing

Contributions, issues, and feature requests are welcome. Feel free to check issues page if you want to contribute.

License

Distributed under the MIT License. See LICENSE for more information.

Keywords

#time_difference #timestamp_comparison #duration_calculation #time_format_conversion #date_difference #interval_measurement #time_unit_conversion #datetime utility