Open In App

JavaScript Date valueOf() Method

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: 




// 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. 




// 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. 




// 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:

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:

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.


Article Tags :