locked
DateDiff in javascript RRS feed

  • Question

  • User1510859543 posted

    Is there an equivalent in javascript to the .net code below to get seconds since 1/1/1970?

    DateDiff(DateInterval.Second, #1/1/1970#, DateTime.Now.ToUniversalTime)

    Monday, August 24, 2020 2:04 PM

Answers

  • User-474980206 posted

    As a JavaScript date is the milliseconds since 1/1/1970, you just divide by 1000.

      var seconds = Date.now() / 1000;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 24, 2020 3:45 PM

All replies

  • User-474980206 posted

    As a JavaScript date is the milliseconds since 1/1/1970, you just divide by 1000.

      var seconds = Date.now() / 1000;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 24, 2020 3:45 PM
  • User475983607 posted

    The getTime() function of the JavaScript Date() returns the milliseconds since 1/1/1970.  The standard docs cover the details. 

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime

    Divide by 1,000 to convert millisecond to seconds.

    var ms = new Date().getTime(); 
    var secs = ms/1000;
    console.log(secs);

    Monday, August 24, 2020 3:51 PM
  • User1510859543 posted

    But does that return Universal time?

    Ignore as it looks like it does.

    Monday, August 24, 2020 3:54 PM