Welcome to our ultimate guide on the JavaScript Date
object and Moment.js. This tutorial will teach you everything you need to know about working with dates and times in your projects.
How to Create a Date
Object
Current Greenwich Mean Time (GMT) The Time Now is a reliable tool when traveling, calling or researching. Installing cucumber in eclipse. The Time Now provides accurate (US network of cesium clocks) synchronized Greenwich Mean Time (GMT).
Get the current date and time
Get a date and time with individual values
Get a date and time with individual values const specifiedDate = new Date(2019, 4, 29, 15, 0, 0, 0); The syntax is Date (year, month, day, hour, minute, second, millisecond). Note that the months are zero-indexed, beginning with January at 0 and ending with December at 11. World clock, time zone converters and meeting planner. Event time & date sharing. Daylight Saving Time dates. Sunrise/sunset and moonrise/moonset. Now a web application.
The syntax is Date(year, month, day, hour, minute, second, millisecond)
.
Note that the months are zero-indexed, beginning with January at 0 and ending with December at 11.
Get a date and time from a timestamp
This represents the time at Thursday, January 1st, 1970 (UTC), or the Unix Epoch time. The Unix Epoch is important because it's what JavaScript, Python, PHP, and other languages and systems use internally to calculate the current time.
new Date(ms)
returns the date of the epoch plus the number of milliseconds you pass in. In a day there's 86,400,000 milliseconds so:
Acronis 2018 true image download. will return Friday, January 2nd, 1970 (UTC).
Get a date and time from a string
Getting the date this way is very flexible. All of the examples below return valid Date
objects:
You can also use the Date.parse()
method to return the number of milliseconds since the epoch (January 1st, 1970):
Setting a time zone
When passing a date string without setting a time zone, JavaScript assumes the date/time are in UTC before converting it to your browser's time zone:
This can lead to errors where the date returned is off by many hours. To avoid this, pass in a time zone along with the string:
You can also pass some, but not all, time zone codes:
Date
Object Methods
Often you will not need the entire date, but just part of it like the day, week or month. Fortunately there are a number of methods to do just that:
Make Working with Dates Easier with Moment.js
Getting dates and times right is no small task. Every country seems to have a different way of formatting dates, and accounting for different time zones and daylight savings/summer time takes, well, a whole lot of time. That's where Moment.js shines – it makes parsing, formatting, and displaying dates a breeze.
To start using Moment.js, install it through a package manager like npm
, or add it to your site through a CDN. See the Moment.js documentation for more details.
Get the current date and time with Moment.js
This returns an object with the date and time based on your browser's location, along with other locale information. It's similar to native JavaScript's new Date()
.
Get a date and time from a timestamp with Moment.js
Similar to new Date(ms)
, you can pass the number of milliseconds since the epoch to moment()
:
If you want to get a date using a Unix timestamp in seconds, you can use the unix()
method:
Get a date and time from a string with Moment.js
Parsing a date from a string with Moment.js is easy, and the library accepts strings in the ISO 8601 or RFC 2822 Date Time format, along with any string accepted by the JavaScript Date
object.
ISO 8601 strings are recommended since it is a widely accepted format. Here are some examples:
Setting a time zone with Moment.js
Up until now, we have been using Moment.js in local mode, meaning that any input is assumed to be a local date or time. This is similar to how the native JavaScript Date
object works:
However, to set a time zone, you must first get the Moment object in UTC mode:
Then you can adjust for the difference in time zones with the utcOffset()
method:
You can also set the UTC offset as a number or a string:
To use named time zones (America/Los_Angeles
) or time zone codes (PDT
) with Moment objects, check out the Moment Timezone library.
Format the date and time with Moment.js
One of the major strengths that Moment.js has over native JavaScript Date
objects is how easy it is to format the output date and time. Just chain the format()
method to a Moment date object and pass it a format string as a parameter:
What Time Zone Is Gmt
Here's a table with some common formatting tokens:
Input | Output | Description |
---|---|---|
YYYY | 2019 | 4 digit year |
YY | 19 | 2 digit year |
MMMM | August | Full month name |
MMM | Aug | Abbreviated month name |
MM | 08 | 2 digit month |
M | 8 | 1 digit month |
DDD | 225 | Day of the year |
DD | 13 | Day of the month |
Do | 13th | Day of the month with ordinal |
dddd | Wednesday | Full day name |
ddd | Wed | Abbreviated day name |
HH | 17 | Hours in 24 hour time |
hh | 05 | Hours in 12 hour time |
mm | 32 | Minutes |
ss | 19 | Seconds |
a | am / pm | Ante or post meridiem |
A | AM / PM | Capitalized ante or post meridiem |
ZZ | +0900 | Timezone offset from UTC |
X | 1410715640.579 | Unix timestamp in seconds |
XX | 1410715640579 | Unix timestamp in milliseconds |
Gmt Time Now
Up until now, we have been using Moment.js in local mode, meaning that any input is assumed to be a local date or time. This is similar to how the native JavaScript Date
object works:
However, to set a time zone, you must first get the Moment object in UTC mode:
Then you can adjust for the difference in time zones with the utcOffset()
method:
You can also set the UTC offset as a number or a string:
To use named time zones (America/Los_Angeles
) or time zone codes (PDT
) with Moment objects, check out the Moment Timezone library.
Format the date and time with Moment.js
One of the major strengths that Moment.js has over native JavaScript Date
objects is how easy it is to format the output date and time. Just chain the format()
method to a Moment date object and pass it a format string as a parameter:
What Time Zone Is Gmt
Here's a table with some common formatting tokens:
Input | Output | Description |
---|---|---|
YYYY | 2019 | 4 digit year |
YY | 19 | 2 digit year |
MMMM | August | Full month name |
MMM | Aug | Abbreviated month name |
MM | 08 | 2 digit month |
M | 8 | 1 digit month |
DDD | 225 | Day of the year |
DD | 13 | Day of the month |
Do | 13th | Day of the month with ordinal |
dddd | Wednesday | Full day name |
ddd | Wed | Abbreviated day name |
HH | 17 | Hours in 24 hour time |
hh | 05 | Hours in 12 hour time |
mm | 32 | Minutes |
ss | 19 | Seconds |
a | am / pm | Ante or post meridiem |
A | AM / PM | Capitalized ante or post meridiem |
ZZ | +0900 | Timezone offset from UTC |
X | 1410715640.579 | Unix timestamp in seconds |
XX | 1410715640579 | Unix timestamp in milliseconds |
Gmt Time Now
Gmt Date And Time Now In America
Richest football players. See the Moment.js docs for more formatting tokens.
What Time Is It In Gmt
Working with JavaScript Date
objects and Moment.js doesn't have to be time consuming. Now you should know more than enough to get started with both.