You can easily create a date with new date() . You can pass parameters to the date constructor to create a date of your choice. The specified parameter can take various forms.
Pass in a date string
You can pass a date string of an accepted format when creating a new date object.
const date = new date (“2020-12-31”);
If we now print the creation date, this will be indicated.
Thu 31.12.2020 01:00:00 GMT + 0100 (Central European Standard Time)
If we want to pass the time or time zone with the parameter string, we can use a format like this.
YYYY-MM-DDTHH: mm: ss.sssZ
- YYYY: year
- MM: month (1 to 12)
- DD: date (1 to 31)
- HH: hour in 24-hour format (0 to 23)
- mm: minutes (0 to 59)
- ss: seconds (00 to 59)
- sss: milliseconds (0 to 999)
- T is used to separate the date and time in the string
- When Z is present, the time is assumed to be in UTC. Otherwise local time is assumed.
However, if T and Z are absent, the date the string was created can produce different results in different browsers. In this case, add to always have the same time zone for the date + HH: mm or -HH: mm until the end.
You can get the same results with that Date.parse Function instead of passing the date string to the date constructor. Date.parse is called indirectly in the constructor when you pass a date string.
The format used in these strings is the ISO 8601 extended calendar format. For details, see the ECMAScript specification .
Pass date arguments
You can pass the date arguments directly to the date constructor without using confusing date strings. The order and length of each year, month, and so on is exactly the same as a date string.
If we check the result of the created date, we can see a crucial difference in the final date.
What’s weird? In creating the date, we used 9 for the month we can assume to be September. When we print the result, the month is October instead. Why is that?
With this method of creating a date, we cannot pass an argument to specify its time zone. Therefore, the system’s local time is used by default. But we can use that Date.UTC Function to convert the date to UTC before it is passed to the date constructor.
Pass in a timestamp
Create a date object for the current date and time
If you want to create a date object for the current system date and time, use the date constructor without passing an argument.
You can use that too Date.now () Function for the same task.
Let’s see how each formatting function works.
ECMAScript Internationalization API Allows a date to be formatted in a specific locale using the Intl object.
You can pass an option object to the DateTimeFormat function to display time values and customize the output.
Custom date formats
If you want to format the date in a different format than what these features provide, you must do so by accessing and combining each part of the date separately.
Now you can convert the date to a custom format using the retrieved parts.
If you want to know whether a certain date is before another, you can directly use operators that are greater or less than to compare.
So, other than Moment.js, what libraries can we use to make our life as developers easier?
Date fns in an open source library that supports date analysis and formatting, locales, and date arithmetic such as addition and subtraction. Because of its versatility, it is known as the Lodash for dates.
Luxon is a date / time manipulation library created by one of the developers of Moment.js to suit modern application needs. Similar to Date-fns, Luxon offers data formatting and analysis functions. It also offers native Intl support and can be linked.
These items was originally published on Live code stream by Juan Cruz Martinez (Twitter: @bajcmartinez), Founder and publisher of Live Code Stream, entrepreneur, developer, author, speaker and maker of things.
Live code stream is also available as a free weekly newsletter. Sign up for updates on everything related to programming, AI and computer science in general.
Read on: Schools of robotic fish could soon monitor our oceans for environmental threats