Convert a Unix timestamp to time in JavaScript

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

    The modern solution that doesn't need a 40 KB library:

    Intl.DateTimeFormat is the non-culturally imperialistic way to format a date/time.

    // Setup once
    var options = {
        //weekday: 'long',
        //month: 'short',
        //year: 'numeric',
        //day: 'numeric',
        hour: 'numeric',
        minute: 'numeric',
        second: 'numeric'
    },
    intlDate = new Intl.DateTimeFormat( undefined, options );
    
    // Reusable formatter
    var timeStamp = 1412743273;
    console.log( intlDate.format( new Date( 1000 * timeStamp ) ) );
    

提交回复
热议问题