Convert a Unix timestamp to time in JavaScript

前端 未结 29 2170
渐次进展
渐次进展 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:40

    function getDateTimeFromTimestamp(unixTimeStamp) {
        let date = new Date(unixTimeStamp);
        return ('0' + date.getDate()).slice(-2) + '/' + ('0' + (date.getMonth() + 1)).slice(-2) + '/' + date.getFullYear() + ' ' + ('0' + date.getHours()).slice(-2) + ':' + ('0' + date.getMinutes()).slice(-2);
    }
    
    const myTime = getDateTimeFromTimestamp(1435986900000);
    console.log(myTime); // output 01/05/2000 11:00

提交回复
热议问题