In this article, we'll:
- Discuss date formatting
- Parse a date object into a number
- Show an alternative way to use arguments instead of strings to create a date object.
Dates are tricky, but they're also incredibly helpful to use. And once you spend a little time going over the basics, your confidence will grow.
// YYYY-MM-DDTHH:mm:ss.sssZ // A date string in ISO 8601 Date Format
How to Use the
If you don't pass anything into the new date constructor, it will give you a date object of whatever the current date and time is.
Note that a date object can, and often should, contain a time down to the millisecond in addition to the month, day, and year.
How to Create a New Date With a String
You may pass a date string into
new Date() to create a date object.
You don't have to specify a time when creating a date object.
new Date('2022-06-13') is perfectly valid. However, when you console log this new date, you'll see that a time will be automatically assigned even though we didn't declare one.
This can create schisms in the matrix, and it is best to include a full date. For instance, since the local system time is used to interpret the date, depending upon where in the world your computer is, you could get different results from the same non-specific date.
So, when passing a string into
new Date(), use a full date with hours:minutes.milliseconds.
A capital T separates the day component from the time component as shown below:
How to Create a New Date With a Number
You can also pass a number into a
new Date() constructor. More on what the numbers represent below – but
new Date(1656033105000), for instance, will return a legitimate date:
How to Create a New Date With Arguments
More on this below too...You many pass up to seven arguments into
new Date() as well, creating a simpler way to represent a date and time to the Date constructor.
What is Date.parse()?
So, an interesting thing happens if you were to use the parse method on a date object. It spits out a huge number.
Date.parse() tells us the number of milliseconds that have elapsed since January 1, 1970. This is helpful when comparing multiple dates. It's easier to compare and measure differences in dates when they are converted to numbers rather than strings.
Which is Better – Dates Made With Arguments or Strings?
new Date(2022, 00, 12, 8, 01, 33, 456)
This can be a bit easier than creating a date using a string. The arguments are simply entered in descending order starting with the year, and ending with the milliseconds.
Only tricky part here: the month is zero indexed. So, January is 00.
This only scratches the surface of the Date object. Check out MDN for a deep dive. As with all things, there is a treasure trove of information there.
new Date(). You can grab the current date and time by not passing anything into the constructor, or you can pass in a string, a number or arguments.
Thanks for Reading
Thanks for reading! I write about design and development here: https://blog.eamonncottrell.com/
Have a great one!