dtdelta

Source:

Calculate date and time differences and query dates and times

Methods

(static) createDelta(delta)

Source:

Crate an object that represents the time delta

The return value has the following keys:

  • delta - relative difference in milliseconds.
  • milliseconds - absolute difference in milliseconds (same as delta).
  • seconds - absolute difference rounded to seconds with no decimals.
  • minutes - absolute difference rounded to minutes with no decimals.
  • hours - absolute difference rounded to hours with no decimals.
  • days - absolute difference rounded to days with no decimals.
  • composite - array of values that represents the total absolute difference ([days, hours, minutes, seconds, milliseconds]).

Relative difference means the difference between two time points relative one of them. This can be a negative or positive number in milliseconds. All other values (including the milliseconds key) are absolute, which means they are always positive, and they are all rounded up.

The composite difference is an array containing the total difference in days, hours, minutes, seconds, and milliseconds.

Main difference between the composite values and the individual keys, is that each individual key is the total absolute difference expressed in given units, while the values in the composite add up to the total.

Example
let dlt = dtdelta.createDelta(3000000);

dlt.delta === 300000;
dtl.days === 1;
dlt.hours === 1;
dlt.minutes === 5;
dlt.seconds === 300;
dlt.milliseconds === 300000;

dlt.composite; // => [0, 0, 5, 0, 0]
Parameters:
Name Type Description
delta number

Time difference in milliseconds

(static) delta(d1, d2opt)

Source:

Calculates the difference between two Date objects

The return value is a delta object (see createDelta). The relative delta between d1 and d2 is calculated relative to d1.

If the second date is omitted, then it defaults to current time (new Date()). This variation tells us how long ago d1 was (if it's in the past).

Example
let d1 = new Date(2019, 4, 12, 14, 50);
let d2 = new Date(2019, 4, 15, 12, 22);

let dlt = dtdelta.delta(d1, d2);

dtl.delta === 286320000;
dtl.days === 4;

dlt.composite; // => [3, 7, 32, 0, 0]
Parameters:
Name Type Attributes Default Description
d1 Date

Reference date object

d2 Date <optional>
new Date

Subject date object

(static) isLeapYear(dt)

Source:

Return true if year of a Date object is leap

Example
dtdelta.isLeapYear(new Date(2000, 0, 1)); // => false
dtdelta.isLeapYear(new Date(2004, 0, 1)); // => true
dtdelta.isLeapYear(new Date(2019, 0, 1)); // => false
Parameters:
Name Type Description
dt Date

The input date