The valueOf method in JavaScript for Date objects retrieves the primitive value associated with a Date instance.
Syntax
The syntax for the valueOf method is illustrated as follows:
Example
dateObj.valueOf()
Return
A primitive value of a Date object.
JavaScript Date valueOf method example
In this section, we will explore the valueOf method by examining a range of examples.
Example 1
Let us examine an instance of retrieving the primitive value from a Date object.
Example
<script>
var date=new Date();
document.writeln(date.valueOf());
</script>
Output:
Output
1533815687148
Example 2
Let’s examine an example that retrieves the primitive value from a given Date object.
Example
<script>
var date=new Date("August 15, 1947 20:22:10 GMT+0530 (India Standard Time) ");
document.writeln(date.valueOf());
</script>
Output:
Output
-706266470000
Example 3
Let us examine an additional example to retrieve the primitive value of a designated Date object.
Example
<script>
var date=new Date(2018, 7, 15, 20, 22, 10);
document.writeln(date.valueOf());
</script>
Output:
Output
1534344730000