Open In App

JavaScript Date valueOf() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The date.valueOf() method is used to get the number of milliseconds between 1 January 1970 00:00:00 UTC and the given date. 

Syntax:

dateObj.valueOf()

Parameters: This method does not accept any parameter. It is just used along with a Date object created using Date() constructor. 

Return Values: It returns the number of milliseconds between 1 January 1970 00:00:00 UTC and the given date as the contents of the Date() constructor. 

Note: The DateObj is a valid Date object created using Date() constructor whose contents are used to get the number of milliseconds between 1 January 1970 00:00:00 UTC and the given date as the contents of the Date() constructor. 

Below are examples of Date.valueOf() method.

Example 1: 

javascript




// Here a date has been assigned
// while creating Date object
let dateobj =
    new Date('October 15, 1996 05:35:32');
 
// Getting the number of milliseconds between
// 1 January 1970 00:00:00
// UTC and the given date as the content of
// the above Date() constructor.
let B = dateobj.valueOf();
 
// Printing the calculated number
// of milliseconds.
console.log(B);


Output:

845337932000

Example 2: If nothing as a parameter is passed while creating a date object but still the valueOf() method returns the number of milliseconds between 1 January 1970 00:00:00 UTC and the current date. 

javascript




// Here nothing has been assigned
// while creating Date object
let dateobj = new Date();
 
// Getting the number of milliseconds between
// 1 January 1970 00:00:00
// UTC and the current date.
let B = dateobj.valueOf();
 
// Printing the calculated number
// of milliseconds.
console.log(B);


Output:

1524387231290

Example 3: Date of a month ranging between 1 to 31. If the date is taken as 35 which is out of the date range, it returns NaN i.e, not a number. 

javascript




// Here a date has been assigned
// while creating Date object
let dateobj = new Date('October 35, 1996 05:35:32');
 
// Getting the number of milliseconds between
// 1 January 1970 00:00:00
// UTC and the given date.
let B = dateobj.valueOf();
 
// Printing the calculated number
// of milliseconds.
console.log(B);


Output:

NaN

Some Important Points:

  • Months, Dates, hours, minutes, seconds, and milliseconds should all be in their respective range. Otherwise valueOf() method returns NaN i.e, not a number.
  • The range of Months, Dates, hours, minutes, seconds, and milliseconds are 0 to 11, 1 to 31, 0 to 23, 0 to 59, 0 to 59, and 0 to 999 respectively.

We have a complete list of Javascript Date Objects, to check those please go through this Javascript Date Object Complete reference article.

Supported Browsers: The browsers supported by JavaScript Date valueOf() method are listed below:

  • Google Chrome
  • Internet Explorer
  • Mozilla Firefox
  • Opera
  • Safari

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.



Last Updated : 22 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Similar Reads