Convert a Unix timestamp to time in JavaScript

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

    Code below also provides 3-digit millisecs, ideal for console log prefixes:

    const timeStrGet = date => {
        const milliSecsStr = date.getMilliseconds().toString().padStart(3, '0') ;
        return `${date.toLocaleTimeString('it-US')}.${milliSecsStr}`;
    };
    
    setInterval(() => console.log(timeStrGet(new Date())), 299);

提交回复
热议问题