
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Current Date and Time in Seconds using JavaScript
In JavaScript, there are built-in methods for getting the current date and time in seconds. We are going to perform this in two ways ?
Let's dive into the article for getting better understanding on getting current date time in seconds.
Using Date.now() method
The Date.now() static method returns the milliseconds that have passed since the epoch, which is recognized as the beginning of January 1, 1970, UTC at midnight.
Syntax
Following is the syntax for Date.now() ?
Date.now()
Example
In the following example, we are running the script using Date.now() to get the date and time in seconds.
<!DOCTYPE html> <html> <body style="background-color:#D5F5E3 "> Current date/time in seconds is: <span class="tutorial"> <button onclick="dateinsec()">Click To Get Date Time In Sec</button> <script> function dateinsec() { dateInMillisecs = Date.now(); dateInSecs = Math.round(dateInMillisecs / 1000); document.querySelector('.tutorial').textContent = dateInSecs; } </script> </span> </body> </html>
When the script gets executed, it will generate an output consisting of the text along with a click button on the webpage. If the user clicks the button, the event gets triggered and displays the date and time in seconds on the webpage.
Using new Date.getTime() method
JavaScript date.getTime() method returns the numeric value corresponding to the time for the specified date according to universal time. The value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00.
Syntax
Following is the syntax for new Date.getTime() ?
Date.getTime()
Example
Consider the following example, here we are running the script using Date.getTime() to get the date and time in seconds.
<!DOCTYPE html> <html> <body style=" text-align:center;background-color:#EBF5FB;"> Current date/time in seconds is: <span class="tutorial"> <button onclick="gettimeinsec()">Get current time</button> <script> function gettimeinsec() { dateInMillisecs = new Date().getTime(); dateInSecs = Math.round(dateInMillisecs / 1000); document.querySelector('.tutorial').textContent = dateInSecs; } </script> </span> </body> </html>
On running the above script, the output window will pop up, displaying the text along with a click button on the webpage. When the user clicks the button, the event gets triggered and gets the time in seconds.
Example
Execute the below code and observe how we are getting the date and time in seconds.
<!DOCTYPE html> <html> <body> <script> var currentDateTime = new Date(); document.write("The current date time is as follows:" + "<br>"); document.write(currentDateTime + "<br>"); var resultInSeconds = currentDateTime.getTime() / 1000; document.write("The current date time in seconds is as follows:" + "<br>") document.write(resultInSeconds); </script> </body> </html>
When the script gets executed, the output window will pop up, triggering the event and displaying the current date and time based on IST and also the date and time in seconds on the webpage.