添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

// Tutorial //

Understanding Date and Time in JavaScript

Published on October 19, 2017 · Updated on August 25, 2021
English
Understanding Date and Time in JavaScript

Introduction

The Date Object

object 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 Date instance 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.

To demonstrate JavaScript’s Date , let’s create a variable and assign the current date to it. This article is being written on Wednesday, October 18th in London (GMT), so that is the current date, time, and timezone that is represented below.

now.js
// Set variable to current date and time const now = new Date ( ) ; // View the output now ;
Output
Wed Oct 18 2017 12:41:34 GMT+0000 (UTC)

Looking at the output, we have a date string containing the following:

Day of the Week Month Minute Second Timezone

The date and time is broken up and printed in a way that we can understand as humans.

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.

// Get the current timestamp
now.getTime();
Output
1508330494000

The large number that appears in our output for the current timestamp represents the same value as above, October 18th, 2017.

Epoch time, also referred to as zero time, is represented by the date string 01 January, 1970 00:00:00 Universal Time (UTC), and by the 0 timestamp. We can test this in the browser by creating a new variable and assigning to it a new Date instance based on a timestamp of 0.

epoch.js
// Assign the timestamp 0 to a new variable const epochTime = new Date(0); epochTime;
Output
01 January, 1970 00:00:00 Universal Time (UTC)

Epoch time was chosen as a standard for computers to measure time by in earlier days of programming, and it is the method that JavaScript uses. It is important to understand the concept of both the timestamp and the date string, as both may be used depending on the settings and purpose of an application.

So far, we learned how to create a new Date instance based on the current time, and how to create one based on a timestamp. In total, there are four formats by which you can create a new Date in JavaScript. In addition to the current time default and timestamp, you can also use a date string, or specify particular dates and times.

Date Creation Output

To demonstrate the different ways to refer to a specific date, we’ll create new Date objects that will represent July 4th, 1776 at 12:30pm GMT in three different ways.

usa.js
// Timestamp method new Date ( - 6106015800000 ) ; // Date string method new Date ( "July 4 1776 12:30" ) ; // Date and time method new Date ( 1776 , 6 , 4 , 12 , 30 , 0 , 0 ) ;

The three examples above all create a date containing the same information.

You’ll notice the timestamp method has a negative number; any date prior to Epoch time will be represented as a negative number.

In the date and time method, our seconds and milliseconds are set to 0 . If any number is missing from the Date creation, it will default to 0 . However, the order cannot be changed, so keep that in mind if you decide to leave off a number. You may also notice that the month of July is represented by 6 , not the usual 7 . This is because the date and time numbers start from 0 , as most counting in programming does. See the next section for a more detailed chart.

Retrieving the Date with get

Modifying the Date with set

Date Methods with UTC

UTC (Coordinated Universal Time) standard. Below is a table of the UTC methods for the JavaScript Date object.

Date/Time Method Range Example

To test the difference between local and UTC get methods, we can run the following code.

UTC.js
// Assign current time to a variable const now = new Date ( ) ; // Print local and UTC timezones console . log ( now . getHours ( ) ) ; console . log ( now . getUTCHours ( ) ) ;

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.

Conclusion

Date reference on the Mozilla Developer Network .

Knowing how to work with dates is essential for many common tasks in JavaScript, as this can enable you to do many things from setting up a repeating report to displaying dates and schedules in the correct time zone.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


Tutorial Series: How To Code in JavaScript

JavaScript is a high-level, object-based, dynamic scripting language popular as a tool for making webpages interactive.

About the authors


Still looking for an answer?

Ask a question Search for more help

Was this helpful?
4 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

this example

new Date("January 31 1980 12:30");

should be

new Date("July 4 1776 12:30");

because

The three examples above all create a date containing the same information.

I don’t think there is a setDay() method.