Skip to content

Simple script to measure time between two points in time and receive results in milliseconds, seconds, minutes, hours, days, weeks or years. Doesn't return negative values, precision can be specified by passing additional argument. For both Node and the browser.

License

Notifications You must be signed in to change notification settings

ecrider/time-between-dates

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Time Between Dates

Simple script to measure time between two dates. Returns result in miliseconds, seconds, minutes, hours, days, weeks or years.

Installation

Install via npm

$ npm install time-between-dates

... or yarn

$ yarn add time-between-dates

then require

var timeBetweenDates = require('time-between-dates');

or import

import timeBetweenDates from 'time-between-dates'

Usage

timeBetweenDates(date, date2, unit, decimal);

Provide two dates, time unit and decimal places are optional

Examples

// some dates
var x = (new Date('November 10, 2017 11:25:24')).getTime();
var y = (new Date('November 15, 2017 05:12:10')).getTime();

// time in miliseconds (default)
timeBetweenDates(x, y);                // returns 409606000

// time in seconds (rounded to fixed)
timeBetweenDates(x, y, 'seconds');     // returns 409606

// time in minutes (one decimal place)
timeBetweenDates(x, y, 'minutes', 1);  // returns 6826.8

// ... and so on
timeBetweenDates(x, y, 'hours', 4);    // returns 113.779
timeBetweenDates(x, y, 'days',  2);    // returns 4.74
timeBetweenDates(x, y, 'weeks', 3);    // returns 0.677
timeBetweenDates(x, y, 'years', 3);    // returns 0.013

// "time since"
var now = Date.now();
timeBetweenDates(x, now, 'days', 2);

Author

Kuba Paczyński

License

Copyright © 2017, Kuba Paczyński. Released under the MIT License.

About

Simple script to measure time between two points in time and receive results in milliseconds, seconds, minutes, hours, days, weeks or years. Doesn't return negative values, precision can be specified by passing additional argument. For both Node and the browser.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published