Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

How to get seconds since epoch in JavaScript?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Given a date, we have to find the number of seconds since the epoch (i.e. 1 January 1970, 00:00:00 UTC ). The getTime() method in the JavaScript returns the number of milliseconds since January 1, 1970, or epoch. If we divide these milliseconds by 1000 and then integer part will give us the number of seconds since epoch.

Example:

Input: Date = 27-04-2020, 11:55:55
Output: Seconds since epoch - 1587968755

Syntax:

new Date(year, month, day, hours, minutes, seconds, milliseconds)

Example:




<script type="text/javascript">
    function seconds_since_epoch(d){ 
        return Math.floor( d / 1000 ); 
    }
      
    // Driver Code
    var d = new Date(2020, 4, 29, 22, 00, 00, 00);
    var sec = seconds_since_epoch(d);
    document.write("Date " + d + " has " 
                   + sec+ " seconds till epoch.");
</script>                    

Output:

Date Fri May 29 2020 22:00:00 GMT+0530 (India Standard Time) 
has 1590769800 seconds till epoch.
My Personal Notes arrow_drop_up
Last Updated : 14 May, 2020
Like Article
Save Article
Similar Reads
Related Tutorials