Convert a Unix timestamp to time in JavaScript

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

    UNIX timestamp is number of seconds since 00:00:00 UTC on January 1, 1970 (according to Wikipedia).

    Argument of Date object in Javascript is number of miliseconds since 00:00:00 UTC on January 1, 1970 (according to W3Schools Javascript documentation).

    See code below for example:

        function tm(unix_tm) {
            var dt = new Date(unix_tm*1000);
            document.writeln(dt.getHours() + '/' + dt.getMinutes() + '/' + dt.getSeconds() + ' -- ' + dt + '
    '); } tm(60); tm(86400);

    gives:

    1/1/0 -- Thu Jan 01 1970 01:01:00 GMT+0100 (Central European Standard Time)
    1/0/0 -- Fri Jan 02 1970 01:00:00 GMT+0100 (Central European Standard Time)
    

提交回复
热议问题