What are JavaScript Dates?
I'm going to let you in on a secret–most developers don't have a great understanding of JavaScript dates. Don't worry if dates confuse you initially. I have an entire post explaining them if you are interested in diving much deeper.
Anyways, here is the quick-start. To create a new date object (remember, new
just creates a "copy" of the Date
"template"):
const now = new Date();
console.log(now); // Thu Jan 14 2021 10:51:27 GMT-0500 (Eastern Standard Time)
The value stored within this date object represents the number of milliseconds that have elapsed since midnight on January 1, 1970, UTC.
You can see that value by using the valueOf()
method.
const now = new Date();
const millisecondsValue = now.valueOf();
console.log(now); // Thu Jan 14 2021 10:53:26 GMT-0500 (Eastern Standard Time)
console.log(millisecondsValue); // 1610639606819
Why January 1st, 1970? What is UTC? Again, read my detailed post if you're curious.
If you want to define a specific date, you can pass a variety of arguments into the Date object. Again, if you want the nitty gritty details, read my post on JS Dates.
// EXAMPLE #1
// Inputs as arguments
// Date(year, month, day, hour, minute, second, millisecond)
// Note: the month is 0-indexed (I have no clue why...)
new Date(2020, 11, 2, 7, 10);
// EXAMPLE #2
// Inputs as various strings
// This works with pretty much anything you can think of
new Date("Jan 20 2020");
new Date("January 20 2020");
new Date("Jan-20-2020");
new Date("Jan 20 2020 02:20:10");
// EXAMPLE #3
// Inputs as numbers (milliseconds)
new Date(102031203);
// EXAMPLE #4
// Inputs as ISO 8601 (we are about to talk about this)
new Date("2020-01-20T00:00Z");
// EXAMPLE #5
// Inputs with timezone specifications
new Date("Jan 20 2020 02:20:10 -10:00"); // SPECIAL CASE
new Date("Jan 20 2020 02:20:10 -1000"); // SPECIAL CASE
new Date("Jan 20 2020 02:20:10 (EDT)"); // SPECIAL CASE
// EXAMPLE #6
// The current moment, specified in the user's local timezone
new Date(Date.now()); // SPECIAL CASE
Some useful Date methods
toString()
toISOString()
getDate()
getMonth()
getFullYear()
These are the common ones. For more, visit the documentation.
Here is a quick example how you can use these methods.
const now = new Date();
// Prints the local date and time
now.toString(); // Thu Jan 14 2021 10:53:26 GMT-0500 (Eastern Standard Time)
// Prints date in ISO8601 format. See - https://cdn-images-1.medium.com/max/2000/1*f1Ye0uCRt1ziCG18sl74CQ.png
now.toISOString(); // 2021-01-14T15:53:26.819Z
now.getDate(); // Returns 14 because I'm writing this on Jan 14, 2021
now.getMonth(); // Returns 0 because the month method is zero-indexed (i.e. Jan = 0, Feb = 1)
now.getFullYear(); // Returns 2021
I think that is enough for now. You don't need to be an expert in JS dates, but definitely need to have some familiarity.
Advanced JavaScript Dates with Timezones
If you want to take your learning even further, I've written a comprehensive post about JavaScript dates that explains how timezones are handled and how to avoid the "off by 1 day" error.