javascript getTime() to 10 digits only

后端 未结 4 489
一整个雨季
一整个雨季 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: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.

提交回复
热议问题