date

Utility methods for working with dates and date ranges

  • knowing some methods might behave incorrectly

See other libraries for more complete functionality:

also watch the TC39 Temporal Proposal

  • also found under caniuse: https://caniuse.com/temporal
See:

Classes

DateRange

Members

(static) TIME

Collection of time durations in milliseconds

Methods

(static) add(dateValue, options) → {Date}

Adds an amount to a date: days, hours, minutes, seconds

d = new Date('2024-12-26 6:00:00');
d30 = utils.date.add(d, { minutes: 30 }); // Date('2024-12-26 6:00:00')
Parameters:
Name Type Description
dateValue Date

date to add to

options Object

options of what to add

Properties
Name Type Attributes Default Description
days Number <optional>
0

number of days to add

minutes Number <optional>
0

number of minutes to add

hours Number <optional>
0

number of minutes to add

seconds Number <optional>
0

number of seconds to add

Returns:
Type
Date

(static) correctForTimezone(date, timeZoneStr) → {Date}

JavaScript always stores dates in UTC, but the data you imported may have lost the timezone information.

Use this to correct the timezone to the correct time UTC.

For Example:

// the date we originally pulled from the database
// but thetimezone of the database was america/Toronto but not in UTC
dateStr = '2024-12-06 18:00';

// we may have done this:
myDate = new Date(dateStr);

// but now calling toISOString() is incorrect
myDate.toISOString(); // '2024-12-06T18:00:00.0000' 

// it should be:
correctedDate = utils.date.correctForTimezone('america/Toronto');
correctedDate.toISOString(); // '2024-12-06T13:00:00.00.000' -- the correct time UTC

See the list of TZ database time zones for the full list of timezone options.

Parameters:
Name Type Description
date Date

the date to be corrected in a new instance

timeZoneStr String

tz database name for the timezone

Returns:
  • copy of the date corrected
Type
Date

(static) durationISO(epochDifference) → {String}

See:
  • DateRange.duration

Prints the duration in ISO format: D:HH:MM:SS.MMM

start = new Date(Date.ISO(2024, 12, 26, 12, 0, 0));
end = new Date(Date.ISO(2024, 12, 26, 13, 0, 0));

duration = end.getTime() - start.getTime();

utils.date.durationLong(duration); // '0 days, 1 hours, 0 minutes, 0.00 seconds'
Parameters:
Name Type Description
epochDifference Number

difference in milliseconds between two dates

Returns:

D days, H hours, M minutes,S.MMMM seconds

Type
String

(static) durationLong(epochDifference) → {String}

See:
  • DateRange.duration

Prints the duration in long format: D days, H hours, M minutes, S.MMM seconds

start = new Date(Date.ISO(2024, 12, 26, 12, 0, 0));
end = new Date(Date.ISO(2024, 12, 27, 13, 0, 0));

duration = end.getTime() - start.getTime();

utils.date.durationLong(duration); // '1 days, 1 hours, 0 minutes, 0.00 seconds'
Parameters:
Name Type Description
epochDifference Number

difference in milliseconds between two dates

Returns:

D days, H hours, M minutes,S.MMMM seconds

Type
String

(static) endOfDay(dateValue) → {Date}

Creates a new date that is at the end of the day (in UTC)

d = new Date('2024-12-26 6:00:00');
dEnd = utils.date.endOfDay(d); // Date('2024-12-26 23:59:59.9999')
Parameters:
Name Type Description
dateValue Date

Date where only the year,month,day is used

Returns:
  • new date set to the end of the day for dateValue's date
Type
Date

(static) epochShift(date, timeZoneStr) → {Date}

See:
  • date.toLocalISO - consider as an alternative. This prints the correct time, without updating the date object.

Epoch shift a date, so the utcDate is no longer correct, but many other functions behave closer to expected.

See here why this might not be what you want

Parameters:
Name Type Description
date Date

date to shift

timeZoneStr String

the tz database name of the timezone

Returns:
Type
Date

(static) getTimezoneOffset(timeZoneStr) → {TimeZoneEntry}

Determines the number of milliseconds difference between a given timezone and UTC.

(Note: these values are cached, and optimized for repeated use on the same value)

See the list of TZ database time zones for the full list of options.

Parameters:
Name Type Description
timeZoneStr String

a timezone string like "America/Toronto"

Returns:
  • the number of milliseconds between UTC and that timezone
Type
TimeZoneEntry

(static) isValid(testDate) → {boolean}

Simple check on whether the a JavaScript Date object is - or is not - an 'Invalid Date' instance.

d = new Date('2024-12-1');
utils.date.isValid(d); // true

d = new Date('2024-12-1T');
utils.date.isValid(d); // false

d = new Date('some string');
utils.date.isValid(d); // false
Parameters:
Name Type Description
testDate Date

JavaScript date to validate

Returns:
  • whether the Date object is an 'invalid date' instance
Type
boolean

(static) parse(dateStr) → {Date}

See:

Harshly parses a JavaScript Date.

If the testValue is null, undefined then the same value is returned.

if the testValue is a valid Date - then the parsed Date object is returned.

If the testValue is not a valid Date, then throws an Error.

d = utils.date.parse('2024-12-01'); // returns Date object
d = utils.date.parse(0); // returns Date object

d = utils.date.parse(null); // returns null
Parameters:
Name Type Description
dateStr String

value passed to Date.parse

Returns:
Type
Date

(static) startOfDay(dateValue) → {Date}

Creates a new date that is at the start of the day (in UTC)

d = new Date('2024-12-26 6:00:00');
dEnd = utils.date.startOfDay(d); // Date('2024-12-26 0:00:00.0000')
Parameters:
Name Type Description
dateValue Date

Date where only the year,month,day is used

Returns:
  • new date set to the end of the day for dateValue's date
Type
Date

(static) toLocalISO(date, timeZoneStr) → {String}

Prints a date in 8601 format to a timezone (with +H:MM offset)

Consider this as an alternative to epochShifting.

d = Date.parse('2024-12-27 13:30:00');

utils.date.toLocalISO(d, 'america/Chicago'); // '2024-12-27T07:30:00.000-06:00'
utils.date.toLocalISO(d, 'europe/paris'); //    '2024-12-27T14:30:00.000+01:00'
Parameters:
Name Type Description
date Date

date to print

timeZoneStr String

the tz database name of the timezone

Returns:
  • ISO format with timezone offset
Type
String

Type Definitions

TimeZoneEntry

Properties:
Name Type Description
tz String

the name of the timezone

formatter function

formats a date to that local timezone

epoch Number

the difference in milliseconds from that tz to UTC

offset String

ISO format for how many hours and minutes offset to UTC '+|-' HH:MMM

Type:
  • Object