Dates and times in JavaScript
Motivation
Dealing with dates and times can be a hustle!
Issues
- Dates are not correct
toLocaleTimeString
gives different results on different machines!
Date not correct!
Check whether one date is before the next doesn’t work reliably!
This SO answer helped by saying one should just
But why does an ISO time string throw an error? i.e. 2010-03-21T00:00:00.000Z
or 2010-03-21T00:00:00.135Z
can’t be properly compared, but 2010-03-21T00:00:00
can?
toLocaleTimeString
craziness!
This function makes me scratch my head!
/**
* Transfrom an ISO time string into the format hh:mm
*/
export const getFormattedTime = (isoTimeString: string): string => {
const date = new Date(isoTimeString);
const formatOptions: Intl.DateTimeFormatOptions = {
hour: '2-digit',
minute: '2-digit',
hour12: false,
};
return date.toLocaleTimeString([], formatOptions);
};
with this test
describe('getFormattedTime', () => {
it.skip('correctly formats a time', () => {
const testIsoTime = '2021-01-13T15:40:35.346660Z';
const result = getFormattedTime(testIsoTime);
// TODO: Investigate - time could differ on other machines.
expect(result).toEqual('16:40');
});
});
- This test is successful on some machines and fail on others.
- There might be an issue with node version but it’s very odd.
Date
object
Decoding of an ISO Date String
-
Example:
2021-02-05T20:01:14.665000Z
Z
means zero hour offset also known as Zulu time (UTC)from this SO answer
If the time is in UTC, add a Z directly after the time without a space. Z is the zone designator for the zero UTC offset. “09:30 UTC” is therefore represented as “09:30Z” or “0930Z”. “14:45:15 UTC” would be “14:45:15Z” or “144515Z”.
taken from here
Convert Date into ISOString:
createdAt: new Date('2021-02-05T20:01:14.665000Z').toISOString(),
Convert ISOString into Date:
// Milliseconds and time-zone identifier Z caused wrong date comparisons
const date = new Date(dateAsIsoString.replace(/\.\d+Z$/, ''));
new Date() vs. Date.now()
Date
can be filled in many different ways.
// year, monthIndex, optionally: day, hours, minutes, seconds, milliseconds
const date = new Date(2010, 12, 31, 15, 30, 0);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date
Examples
isToday
const isToday = (date: Date): boolean => {
const today = new Date()
return date.getDate() === today.getDate() &&
date.getMonth() === today.getMonth() &&
date.getFullYear() === today.getFullYear();
};
Discuss on Twitter ● Improve this article: Edit on GitHub