JavaScript Dates and Times
dates & times
JavaScript defines a Date class for representing and manipulating the numbers to represent dates and times.
We can create a date object with the Date() constructor. We can use this object to get year, month and day to display like a timer in a webpage.
The result of the Date() constructor varies with the arguments we passed into it.
For example,
- constructor with no argument (Date())— give current time.
- constructor with one argument (Date(milliseconds))— interprets that argument as the number of milliseconds since the 1970 epoch.
- constructor with two or more arguments (Date(year, month, day, hours, minutes, seconds, milliseconds)) — interpreted as the year, month, day-of-month, hour, minute, second, and millisecond in your local time zone.
In Date API, first month of the year is denoted as 0 and first day of the month is denoted as 1.
The Date() constructor interprets the arguments using the local computer time zone.
My local computer is set to India Standard Time, so the dates and times in this article are interpreted in India Standard Time.
We add a specific time zone to specify the date and time in that time zone.
Example: Adding UTC time zone
Date.UTC() — this will take the same arguments as the Date() constructor and interprets them in UTC, and returns a millisecond timestamp that you can pass to the Date() constructor. But the results by default will be displayed in local computer time zone. To display the date in UTC, we have to apply toString(), toUTCString() or toISOString().
JavaScript Date Methods
There are get and set methods to query and modify year, month, day-of-month, hours, minutes, seconds, and milliseconds fields of the Date.
These methods can be used in two ways, using local time and UTC time.
- To get full year of the Date we can use
getFullYear()
getUTCFullYear()
- To set full year of the Date we can use
setFullYear()
setUTCFullYear()
to get and set other fields of the Date we have to replace the above mentioned get and set methods ‘FullYear’ with the particular field name.(Example getMonth, getUTCMonth, setMonth, setUTCMonth)
Note :
methods for querying
the day-of-month — — → getDate() and getUTCDate().
day-of-week (0 for Sunday through 6 for Saturday) — — → getDay() and getUTCDay() (The day-of-week is read-only, so there is not a corresponding setDay() method.)
Timestamps
JavaScript represents dates internally as integers that specify the number of milliseconds since (or before) midnight on January 1, 1970, UTC time. This is called timestamps. The static Date.now() method returns the current time as a timestamp.
Formatting and Parsing date strings
There string formatting methods to the Date class.
toString() — This method uses the local time zone but does not format the date and time in a locale aware way.
toUTCString() — This method uses the UTC time zone but does not format the date in a locale-aware way.
toISOString() — This method prints the date and time in the standard year-month-day hours:minutes:seconds.ms format of the ISO-8601 standard. The letter “T” separates the date portion of the output from the time portion of the output. The time is expressed in UTC, and this is indicated with the letter “Z” as the last letter of the output.
toLocaleString() — This method uses the local time zone and a format that is appropriate for the user’s locale.
toDateString() — This method formats only the date portion of the Date and omits the time. It uses the local time zone and does not do locale appropriate formatting.
toLocaleDateString() — This method formats only the date. It uses the local time zone and a locale appropriate date format.
toTimeString() — This method formats only the time and omits the date. It uses the local time zone but does not format the time in a locale aware way.
toLocaleTimeString() — This method formats the time in a locale aware way and uses the local time zone.
There is a static method called parse() in Date object which takes a string representation of a date as an argument and returns a timestamp representing the date since January 1, 1970, 00:00:00 UTC or NaN, if the string is unrecognized or unsuitable date values.
More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord. Interested in Growth Hacking? Check out Circuit.