JavaScript setTime function is a Date Function, which is used to set the Time by adding user-specified milliseconds to a default date, January 1, 1970, 00:00:00. Here, you have to specify the time in Milliseconds, and it is an optional argument. The syntax of the setTime function is:
Date.setTime(Milliseconds)
JavaScript setTime Function Example
Here, we use this setTime function to set the Milliseconds to 20000000.
<!DOCTYPE html> <html> <head> <title> JS </title> </head> <body> <h1> Example </h1> <script> var dt = Date(); document.write("Date and Time : " + dt + "<br/>"); dt.setTime(20000000); document.write("After : " + dt); </script> </body> </html>
Example
Date and Time: Thu Nov 08 2018 12:06:15 GMT+0530 (Indian Standard Time)
After: Thu Jan 01 1970 11:03:20 GMT+0530 (Indian Standard Time)
Example 2
In this JavaScript set Time function example, we set the Time of a custom date to 2000 milliseconds.
<!DOCTYPE html> <html> <head> <title> JavaScriptSetTimeFunctions </title> </head> <body> <h1> JavaScriptsetTimeFunctionExample </h1> <script> var dt = Date("January 1, 2017 23:45:22"); document.write("Date and Time : " + dt + "<br/>"); dt.setTime(2000); document.write("After setTime() : " + dt); </script> </body> </html>