JavaScript | date.valueOf() function
The date.valueOf() is an inbuilt function in JavaScript which is used to get the number of milliseconds between 1 January 1970 00:00:00 UTC and the given date.
Syntax:
dateObj.valueOf()
Note: In the above syntax, DateObj is a valid Date object created using Date() conctructor 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.
- This function does not takes any parameter. It is just used along with a Date object created using Date() conctructor.
- 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.
Parameters:
Return Values:
Below program illustrate the date.valueOf() function:-
<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
- If nothing as parameter is passed while creating date object but still valueOf() function returns the number of milliseconds between 1 January 1970 00:00:00 UTC and the current date.
<script>// Here nothing has been assigned// while creating Date objectvardateobj =newDate();// Getting the number of milliseconds between 1 January 1970 00:00:00// UTC and the current date.varB = dateobj.valueOf();// Printing the calculated number of milliseconds.document.write(B);</script>chevron_rightfilter_noneOutput:
> 1524387231290
- 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.
<script>// Here a date has been assigned// while creating Date objectvardateobj =newDate('October 35, 1996 05:35:32');// Getting the number of milliseconds between 1 January 1970 00:00:00// UTC and the given date.varB = dateobj.valueOf();// Printing the calculated number of milliseconds.document.write(B);</script>chevron_rightfilter_noneOutput:
> NaN
Errors and Exceptional cases associated with this function.
Some Important Points:
- Months, Dates, hours, minutes, seconds, milliseconds should all be in their respective range.Otherwise valueOf() function 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.
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- JavaScript | Function Parameters
- JavaScript | Function Definitions
- JavaScript | toString( ) function
- JavaScript | Math.pow( ) Function
- JavaScript | Array every() function
- JavaScript | String() Function
- JavaScript | Function Apply
- JavaScript | Function Call
- Javascript | Number() Function
- JavaScript | Math.E() function
- JavaScript | Array some() function
- JavaScript | toFixed( ) Function
- JavaScript | Array.of() function
- JavaScript | Math.abs( ) function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



