javascript getTime() to 10 digits only

后端 未结 4 446
一整个雨季
一整个雨季 2021-01-12 03:07

I am using the following function to get the Time using javascript:

function timeMil(){
    var date = new Date();
    var timeMil = date.getTime();

    ret         


        
相关标签:
4条回答
  • 2021-01-12 03:45

    I think you just have to divide it by 1000 milliseconds and you'll get time in seconds

    Math.floor(date.getTime()/1000)
    
    0 讨论(0)
  • 2021-01-12 03:51

    If brevity is ok, then:

    function secondsSinceEpoch() {
        return new Date/1000 | 0;
    }
    

    Where:

    • new Date is equivalent to new Date()
    • | 0 truncates the decimal part of the result and is equivalent to Math.floor(new Date/1000) (see What does |0 do in javascript).

    Using newer features, and allowing for a Date to be passed to the function, the code can be reduced to:

    let getSecondsSinceEpoch = (x = new Date) => x/1000 | 0;
    

    But I prefer function declarations as I think they're clearer.

    0 讨论(0)
  • 2021-01-12 04:03

    You could divide by 1000 and use Math.floor() on JavaScript.

    0 讨论(0)
  • 2021-01-12 04:04

    Try dividing it by 1000, and use parseInt method.

    const t = parseInt(Date.now()/1000);
    
    console.log(t);
    
    0 讨论(0)
提交回复
热议问题