Start sharing components as a team!Share components as a team!Join Bit to build your applications faster.Get Started Free

date-diff

v0.0.6arrow_drop_down
v0.0.6
v0.0.5
v0.0.4
v0.0.3
v0.0.2
v0.0.1
STATUS
Passing
DOWNLOADS
25
LICENSE
MIT
VISIBILITY
Public
PUBLISHED
3 years ago
SIZE
682 B
Calculates the time difference between two date objects in the provided time unit.
Install date-diff as a package?
Copied
npm i @bit/gideonshils.js-methods.time.date-diff
Set Bit as a scoped registryLearn more
npm config set '@bit:registry' https://node.bit.dev
Component Example
React
React
Vue
Angular
React Native
Add dependency... help_outline
Just
import
any of the 1 million components
and packages in Bit or NPM to the example.
import Button from '@bit/grommet.grommet.button';
import Lodash from 'lodash';
toggle layout
var date1 = new Date('July 1, 1989 09:41:00);
var date2 = new Date('June 29, 2011 18:45:10');
dateDiff(date1, date2, 'milliseconds'); // => 694083850000
dateDiff(date1, date2, 'seconds');      // => 694083850
dateDiff(date1, date2, 'minutes');      // => 11568064
dateDiff(date1, date2, 'hours');        // => 192801
dateDiff(date1, date2, 'days');         // => 8033
dateDiff(date1, date2, 'weeks');        // => 1148
dateDiff(date1, date2, 'months');       // => 263
dateDiff(date1, date2, 'years');        // => 22
dateDiff(date1, date2);                 // => 694083850000
dateDiff('Not a date');                 // => undefined (Incorrect input types)
modifieddraft
chevron_left
chevron_right
dateDiff (
date1:Object,
date2:Object,
unit:string
) : number

Calculates the time difference between two date objects in the provided time unit. Rounds decimals. Supports milliseconds, seconds, minutes, hours, days, weeks, months, and years.

Example
```js
var date1 = new Date('July 1, 1989 09:41:00);
var date2 = new Date('June 29, 2011 18:45:10');

dateDiff(date1, date2, 'milliseconds'); // => 694083850000
dateDiff(date1, date2, 'seconds');      // => 694083850
dateDiff(date1, date2, 'minutes');      // => 11568064
dateDiff(date1, date2, 'hours');        // => 192801
dateDiff(date1, date2, 'days');         // => 8033
dateDiff(date1, date2, 'weeks');        // => 1148
dateDiff(date1, date2, 'months');       // => 263
dateDiff(date1, date2, 'years');        // => 22

dateDiff(date1, date2);                 // => 694083850000
dateDiff('Not a date');                 // => undefined (Incorrect input types)
```
Arguments
date1: Object

The first date

date2: Object

The second date

unit: string

(optional) The unit to use. Defaults to milliseconds

Returns
number

The time difference

Test Summary

dateDiff() should return a positive integer when date1 < date2

Pass

dateDiff() should return a negative integer when date1 > date2

Pass

dateDiff() should return zero when the dates are identical

Pass

dateDiff() should return undefined when either of the first two arguments are not Date objects

Pass

dateDiff() should return result in milliseconds when no time unit is specified

Pass

dateDiff() should return result in milliseconds when an unsupported time unit is specified

Pass