How do you get a timestamp in JavaScript?

前端 未结 30 3121
情深已故
情深已故 2020-11-21 15:19

How can I get a timestamp in JavaScript?

Something similar to Unix timestamp, that is, a single number that represents the current time and date. Either as a number

相关标签:
30条回答
  • 2020-11-21 15:32

    Just to add up, here's a function to return a timestamp string in Javascript. Example: 15:06:38 PM

    function displayTime() {
        var str = "";
    
        var currentTime = new Date()
        var hours = currentTime.getHours()
        var minutes = currentTime.getMinutes()
        var seconds = currentTime.getSeconds()
    
        if (minutes < 10) {
            minutes = "0" + minutes
        }
        if (seconds < 10) {
            seconds = "0" + seconds
        }
        str += hours + ":" + minutes + ":" + seconds + " ";
        if(hours > 11){
            str += "PM"
        } else {
            str += "AM"
        }
        return str;
    }
    
    0 讨论(0)
  • 2020-11-21 15:32

    One I haven't seen yet

    Math.floor(Date.now() / 1000); // current time in seconds
    

    Another one I haven't seen yet is

    var _ = require('lodash'); // from here https://lodash.com/docs#now
    _.now();
    
    0 讨论(0)
  • 2020-11-21 15:32

    more simpler way:

    var timeStamp=event.timestamp || new Date().getTime();
    
    0 讨论(0)
  • 2020-11-21 15:33

    You can only use

        var timestamp = new Date().getTime();
        console.log(timestamp);

    to get the current timestamp. No need to do anything extra.

    0 讨论(0)
  • 2020-11-21 15:33

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

    0 讨论(0)
  • 2020-11-21 15:34

    Short & Snazzy:

    + new Date()
    

    A unary operator like plus triggers the valueOf method in the Date object and it returns the timestamp (without any alteration).

    Details:

    On almost all current browsers you can use Date.now() to get the UTC timestamp in milliseconds; a notable exception to this is IE8 and earlier (see compatibility table).

    You can easily make a shim for this, though:

    if (!Date.now) {
        Date.now = function() { return new Date().getTime(); }
    }
    

    To get the timestamp in seconds, you can use:

    Math.floor(Date.now() / 1000)
    

    Or alternatively you could use:

    Date.now() / 1000 | 0
    

    Which should be slightly faster, but also less readable (also see this answer).

    I would recommend using Date.now() (with compatibility shim). It's slightly better because it's shorter & doesn't create a new Date object. However, if you don't want a shim & maximum compatibility, you could use the "old" method to get the timestamp in milliseconds:

    new Date().getTime()
    

    Which you can then convert to seconds like this:

    Math.round(new Date().getTime()/1000)
    

    And you can also use the valueOf method which we showed above:

    new Date().valueOf()
    

    Timestamp in Milliseconds

    var timeStampInMs = window.performance && window.performance.now && window.performance.timing && window.performance.timing.navigationStart ? window.performance.now() + window.performance.timing.navigationStart : Date.now();
    
    console.log(timeStampInMs, Date.now());

    0 讨论(0)
提交回复
热议问题