Date
Times are represented in native JavaScript with the Date
object.
For things not covered by the native Date, see date-fns.
Syntax
new Date()
new Date(dateString)
new Date(dateObject)
new Date(year, monthIndex)
new Date(year, monthIndex, day, ...)
Example:
// Will get the system's current date/time as UTC
const now = new Date();
// Can pass in specific values to make a specific date
// Months are 0-indexed
const birthday = new Date(1999, 8, 15);
Date
s can be constructed with specific times as well.
Date parsing
Sometimes you'll be given a value, and need to convert that into a Date object. Unfortunately, outside of the Date time string format this is not guaranteed support at all.
This means that you'll often be required to parse your own string, into a date.
Timezones
Timezones are a bastard, and always have been in JavaScript.
For dealing with them, you could go down the native Intl
route (if supported) or the library route.
Some libraries include:
Print a Date in a given timezone
You can specify custom behaviour for printing out Date objects using the Intl.DateTimeFormat
builtin.
Example:
const now = new Date();
const local = new Intl.DateTimeFormat('en-GB', {
timeZone: 'Australia/Melbourne',
dateStyle: 'full',
timeStyle: 'long'
});
// 'Thursday, 20 July 2023 at 13:41:16 GMT+10'
local.format(date);
Get current browser timezone
To get the current browser Timezone, use the Intl
builtin resolvedOptions()
.
// Australia/Melbourne
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
Temporal
Temporal is a proposed new namespace to be added into ECMAScript, which hopes to fix the glaring issues with the Date prototype.
Moment.js
Moment.js was previously the go-to standard library for using dates in JavaScript.
It has since been thoroughly deprecated, to the point where Moment.js themselves recommend abandoning the project.
For modern date libraries:
- If you're looking for a more drop-in replacement, pick Day.js
- If you get to pick a new one, pick Adobe Internationalized
- If the vanilla JS Date is good enough, pick date-fns