How to work with Date and time in Javascript ??
- A JavaScript date is fundamentally specified as the number of milliseconds that have elapsed since midnight on January 1, 1970, UTC.
- This date and time is the same as the UNIX epoch.
DATE OBJECT
- The
Dateobject is a built-in object in JavaScript that stores the date and time. It provides a number of built-in methods for formatting and managing that data. - By default, a new
Dateinstance without arguments provided creates an object corresponding to the current date and time. This will be created according to the current computer’s system settings..
| Day of the Week | Month | Day | Year | Hour | Minute | Second | Timezone |
|---|---|---|---|---|---|---|---|
| thu | Jun | 11 | 2020 | 20 | 22 | 52 | GMT+0000 (UTC) |
- JavaScript, however, understands the date based on a timestamp derived from Unix time, which is a value consisting of the number of milliseconds that have passed since midnight on January 1st, 1970. We can get the timestamp with the
getTime()method.
Retrieving the Date with get()
Once we have a date, we can access all the components of the date with various built-in methods. The methods will return each part of the date relative to the local timezone. Each of these methods starts with get, and will return the relative number. Below is a detailed table of the get methods of the Date object.
| Date/Time | Method | Range | Example |
|---|---|---|---|
| Year | getFullYear() | YYYY | 1970 |
| Month | getMonth() | 0-11 | 0 = January |
| Day (of the month) | getDate() | 1-31 | 1 = 1st of the month |
| Day (of the week) | getDay() | 0-6 | 0 = Sunday |
| Hour | getHours() | 0-23 | 0 = midnight |
| Minute | getMinutes() | 0-59 | |
| Second | getSeconds() | 0-59 | |
| Millisecond | getMilliseconds() | 0-999 | |
| Timestamp | getTime() | Milliseconds since Epoch time |
Date methods that begin with get allow us to access date components that return the number associated with what we are retrieving from the instantiated object.Date Methods with UTC
The get methods discussed above retrieve the date components based on the user’s local timezone settings. For increased control over the dates and times, you can use the getUTC methods, which are exactly the same as the get methods, except they calculate the time based on the UTC (Coordinated Universal Time) standard. Below is a table of the UTC methods for the JavaScript Date object.
| Date/Time | Method | Range | Example |
|---|---|---|---|
| Year | getUTCFullYear() | YYYY | 1970 |
| Month | getUTCMonth() | 0-11 | 0 = January |
| Day (of the month) | getUTCDate() | 1-31 | 1 = 1st of the month |
| Day (of the week) | getUTCDay() | 0-6 | 0 = Sunday |
| Hour | getUTCHours() | 0-23 | 0 = midnight |
| Minute | getUTCMinutes() | 0-59 | |
| Second | getUTCSeconds() | 0-59 | |
| Millisecond | getUTCMilliseconds() | 0-999 |
To test the difference between local and UTC get methods, we can run the following code.
Running this code will print out the current hour, and the hour of the UTC timezone. If you are currently in the UTC timezone the numbers that are output from running the program above will be the same.
UTC is useful in that it provides an international time standard reference and can therefore keep your code consistent across timezones if that is applicable to what you are developing.
Modifying the Date with set
For all the get methods that we learned about above, there is a corresponding set method. Where get is used to retrieve a specific component from a date, set is used to modify components of a date. Below is a detailed chart of the set methods of the Date object.
| Date/Time | Method | Range | Example |
|---|---|---|---|
| Year | setFullYear() | YYYY | 1970 |
| Month | setMonth() | 0-11 | 0 = January |
| Day (of the month) | setDate() | 1-31 | 1 = 1st of the month |
| Day (of the week) | setDay() | 0-6 | 0 = Sunday |
| Hour | setHours() | 0-23 | 0 = midnight |
| Minute | setMinutes() | 0-59 | |
| Second | setSeconds() | 0-59 | |
| Millisecond | setMilliseconds() | 0-999 | |
| Timestamp | setTime() | Milliseconds since Epoch time |
We can use these set methods to modify one, more, or all of the components of a date.
toLocateTimeString():
Return the time portion of a Date object as a string, using locale conventions:
var d = new Date();
var n = d.toLocaleTimeString();
Comments
Post a Comment