Date.difference >= 0.1.8

Purpose

Determine the difference between two dates

Syntax

Date.difference ( StringunitDatestartDateendBooleanstartOfUnit= false );

Parameters

unit

start

end

startOfUnit

Return values

Number

Examples

Return the differences

var a = new Date("2015-08-26T14:39:05.745Z"),
    b = new Date("2015-08-14T10:31:10.045Z");

// When no units is given, ms is used
Date.difference(a, b);
>>> -1051675700

// You can also supply seconds
Date.difference('seconds', a, b);
>>> -1051675.7

Date.difference('minutes', a, b);
>>> -17527.928333333333

Date.difference('hours', a, b);
>>> -292.1321388888889

Date.difference('days', a, b);
>>> -12.172172453703704

Date.difference('weeks', a, b);
>>> -1.738881779100529

Date.difference('months', a, b);
>>> -0.40573908179012347

Date.difference('years', a, b);
>>> -0.03334841768138001

Return the difference from the start of the unit

var a = new Date("2015-08-26T14:39:05.745Z"),
    b = new Date("2015-08-14T10:31:10.045Z");

// Will be calculated between "2015-08-26T14:39:05.000Z" and "2015-08-14T10:31:10.000Z"
Date.difference('seconds', a, b, true);
>>> -1051675

// Will be calculated between "2015-08-26T14:39:00.000Z" and "2015-08-14T10:31:00.000Z"
Date.difference('minutes', a, b, true);
>>> -17528

// Will be calculated between "2015-08-26T14:00:00.000Z" and "2015-08-14T10:00:00.000Z"
Date.difference('hours', a, b, true);
>>> -292

Date.difference('days', a, b, true);
>>> -12

Date.difference('weeks', a, b, true);
>>> -2

Date.difference('months', a, b, true);
>>> 0

Date.difference('years', a, b, true);
>>> 0

Calculate between date strings

Date.difference('hours', "2015-08-26T14:00:00.000Z", "2015-08-14T10:00:00.000Z");
>>> -292

Comments