JavaScript Date valueOf() Method

17 Jan, 2022
Below is the example of Date valueOf() method.
Example:javascriptjavascript<script>   // Here a date has been assigned   // while creating Date object   var 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.   var B = dateobj.valueOf();     // Printing the calculated number   // of milliseconds.   document.write(B);</script>
javascript
<script>   // Here a date has been assigned   // while creating Date object   var 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.   var B = dateobj.valueOf();     // Printing the calculated number   // of milliseconds.   document.write(B);</script>
Output:845337932000
845337932000
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.
More codes for the above method are as follows:
Program 1: If nothing as a parameter is passed while creating date object but still valueOf() method returns the number of milliseconds between 1 January 1970 00:00:00 UTC and the current date.
javascript
<script>   // Here nothing has been assigned   // while creating Date object   var dateobj = new Date();     // Getting the number of milliseconds between    // 1 January 1970 00:00:00   // UTC and the current date.   var B = dateobj.valueOf();     // Printing the calculated number    // of milliseconds.   document.write(B);</script>
1524387231290
Program 2: 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
<script>   // Here a date has been assigned   // while creating Date object   var 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.   var B = dateobj.valueOf();     // Printing the calculated number    // of milliseconds.   document.write(B);</script>
NaN
Some Important Points:
Months, Dates, hours, minutes, seconds, milliseconds should all be in their respective range.Otherwise valueOf() method returns NaN i.e, not a number.
Range of Months, Dates, hours, minutes, seconds, milliseconds are 0 to 11, 1 to 31, 0 to 23, 0 to 59, 0 to 59, 0 to 999 respectively.
Supported Browsers: The browsers supported by JavaScript Date valueOf() Method are listed below:
Google Chrome 1 and above
Edge 12 and above
Firefox 1 and above
Internet Explorer 4 and above
Opera 3 and above
Safari 1 and above
ysachin2314
surindertarika1234
javascript-date
JavaScript-Methods
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.