How to get seconds since epoch in JavaScript?

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>                    

chevron_right


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

Check out this Author's contributed articles.

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.