Get current date/time in seconds

前端 未结 13 1590
失恋的感觉
失恋的感觉 2020-11-29 17:14

How do I get the current date/time in seconds in Javascript?

相关标签:
13条回答
  • 2020-11-29 17:34

    On some day in 2020, inside Chrome 80.0.3987.132, this gives 1584533105

    ~~(new Date()/1000) // 1584533105
    Number.isInteger(~~(new Date()/1000)) // true
    
    0 讨论(0)
  • 2020-11-29 17:36

    These JavaScript solutions give you the milliseconds or the seconds since the midnight, January 1st, 1970.

    The IE 9+ solution(IE 8 or the older version doesn't support this.):

    var timestampInMilliseconds = Date.now();
    var timestampInSeconds = Date.now() / 1000; // A float value; not an integer.
        timestampInSeconds = Math.floor(Date.now() / 1000); // Floor it to get the seconds.
        timestampInSeconds = Date.now() / 1000 | 0; // Also you can do floor it like this.
        timestampInSeconds = Math.round(Date.now() / 1000); // Round it to get the seconds.
    

    To get more information about Date.now(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now

    The generic solution:

    // ‘+’ operator makes the operand numeric.
    // And ‘new’ operator can be used without the arguments ‘(……)’.
    var timestampInMilliseconds = +new Date;
    var timestampInSeconds = +new Date / 1000; // A float value; not an intger.
        timestampInSeconds = Math.floor(+new Date / 1000); // Floor it to get the seconds.
        timestampInSeconds = +new Date / 1000 | 0; // Also you can do floor it like this.
        timestampInSeconds = Math.round(+new Date / 1000); // Round it to get the seconds.
    

    Be careful to use, if you don't want something like this case.

    if(1000000 < Math.round(1000000.2)) // false.
    
    0 讨论(0)
  • 2020-11-29 17:38
     Date.now()
    

    gives milliseconds since epoch. No need to use new.

    Check out the reference here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now

    (Not supported in IE8.)

    0 讨论(0)
  • 2020-11-29 17:41

    Based on your comment, I think you're looking for something like this:

    var timeout = new Date().getTime() + 15*60*1000; //add 15 minutes;
    

    Then in your check, you're checking:

    if(new Date().getTime() > timeout) {
      alert("Session has expired");
    }
    
    0 讨论(0)
  • 2020-11-29 17:41

    // The Current Unix Timestamp
    // 1443535752 seconds since Jan 01 1970. (UTC)
    
    // Current time in seconds
    console.log(Math.floor(new Date().valueOf() / 1000));  // 1443535752
    console.log(Math.floor(Date.now() / 1000));            // 1443535752
    console.log(Math.floor(new Date().getTime() / 1000));  // 1443535752
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    jQuery

    console.log(Math.floor($.now() / 1000));               // 1443535752
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    0 讨论(0)
  • 2020-11-29 17:42

    Using new Date().getTime() / 1000 is an incomplete solution for obtaining the seconds, because it produces timestamps with floating-point units.

    const timestamp = new Date() / 1000; // 1405792936.933
    // Technically, .933 would be milliseconds. 
    

    A better solution would be:

    // Rounds the value
    const timestamp = Math.round(new Date() / 1000); // 1405792937
    
    // - OR -
    
    // Floors the value
    const timestamp = new Date() / 1000 | 0; // 1405792936
    

    Values without floats are also safer for conditional statements, as the float may produce unwanted results. The granularity you obtain with a float may be more than needed.

    if (1405792936.993 < 1405792937) // true
    
    0 讨论(0)
提交回复
热议问题