Convert a Unix timestamp to time in JavaScript

前端 未结 29 2173
渐次进展
渐次进展 2020-11-21 04:57

I am storing time in a MySQL database as a Unix timestamp and that gets sent to some JavaScript code. How would I get just the time out of it?

For example, in HH/MM/

29条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-21 05:34

    This works with PHP timestamps

    var d = 1541415288860;
    //var d =val.timestamp;
    
    //NB: use + before variable name
    var date = new Date(+d);
    
    console.log(d);
    console.log(date.toDateString());
    console.log(date.getFullYear());
    console.log(date.getMinutes());
    console.log(date.getSeconds());
    console.log(date.getHours());
    console.log(date.toLocaleTimeString());

    var d =val.timestamp;
    var date=new Date(+d); //NB: use + before variable name
    
    console.log(d);
    console.log(date.toDateString());
    console.log(date.getFullYear());
    console.log(date.getMinutes());
    console.log(date.getSeconds());
    console.log(date.getHours());
    console.log(date.toLocaleTimeString());
    

    the methods above will generate this results

    1541415288860
    Mon Nov 05 2018 
    2018 
    54 
    48 
    13
    1:54:48 PM
    

    There's a bunch of methods that work perfectly with timestamps. Cant list them all

提交回复
热议问题