Javascript Date Type

Introduction

The Javascript Date type stores dates as the number of milliseconds that have passed since midnight on January 1, 1970 UTC (Universal Time Code).

Javascript Date type has the following features:

  • Javascript Date has both date and time.
  • Date objects always carry both.
  • Months are counted from zero (January is 0).
  • Days of week in getDay() are counted from zero (0 is Sunday).
  • Date auto-corrects itself when out-of-range components are set.
  • Dates can be subtracted, giving their difference in milliseconds.
  • Dates become the timestamp when converted to a number.
  • Date.now() can get the current timestamp faster.

To create a date object, use the new operator along with the Date constructor, like this:

let now = new Date(); 

When the Date constructor is used without any arguments, the created object is assigned the current date and time.

To create a date based on another date or time, pass in the millisecond representation of the date.

The Date.parse() method accepts a string argument representing a date.

It attempts to convert the string into a millisecond representation of a date.

All implementations now support the following date formats:

  • Format Example
  • month/date/year 5/23/2020
  • month_name date, year May 23, 2020
  • day_of_week month_name date year hours:minutes:seconds time_zone Tue May 23 2020 00:00:00 GMT-0700
  • ISO 8601 extended format YYYY-MM-DDTHH:mm:ss.sssZ 2019-05-23T00:00:00

For instance, to create a date object for May 23, 2020, you can use the following code:

let someDate = new Date(Date.parse("May 23, 2020")); 

If the string passed into Date.parse() doesn't represent a date, then it returns NaN.

The Date constructor will call Date.parse() behind the scenes if a string is passed in directly.

The following code is identical to the previous example:

let someDate = new Date("May 23, 2020"); 

The Date.UTC() method returns the millisecond representation of a date.

The arguments for Date.UTC() are the year, the zero-based month (January is 0, February is 1, and so on), the day of the month (1 through 31), and the hours (0 through 23), minutes, seconds, and milliseconds of the time.

Of these arguments, only the first two (year and month) are required.

If the day of the month isn't supplied, it's assumed to be 1, while all other omitted arguments are assumed to be 0. Here are two examples of Date.

// January 1, 2000 at midnight GMT 
let y2k = new Date(Date.UTC(2000, 0)); 
                
// May 5, 2005 at 5:55:55 PM GMT 
let allFives = new Date(Date.UTC(2005, 4, 5, 17, 55, 55)); 

The first date is for midnight (GMT) on January 1, 2000.

The second date represents May 5, 2005, at 5:55:55 PM GMT.

Date.UTC() is mimicked by the Date constructor but one major difference: the date and time created are in the local time zone, not in GMT.

let allFives = new Date(Date.UTC(2020, 4, 5, 17, 55, 55)); 
console.log(allFives);// ww  w . ja  v  a2s.com
allFives = new Date(2020, 4, 5, 17, 55, 55); 
console.log(allFives);

The Date constructor takes the same arguments as Date.UTC(), so if the first argument is a number, the constructor assumes that it is the year of a date, the second argument is the month, and so on.

The preceding example can then be rewritten as this:

// January 1, 2000 at midnight in local time 
let y2k = new Date(2000, 0); 
                
// May 5, 2005 at 5:55:55 PM local time 
let allFives = new Date(2005, 4, 5, 17, 55, 55); 

Javascript also offers Date.now(), which returns the millisecond representation of the date and time at which the method is executed.

This method makes it trivial to use Date objects for code profiling, such as:

// get start time 
let start = Date.now();

// call a function 
doSomething();

// get stop time 
let stop = Date.now(),
result = stop - start;



PreviousNext

Related