我将时间作为 Unix 时间戳存储在 MySQL 数据库中,并将其发送到一些 JavaScript 代码。我怎样才能从中抽出时间呢?
例如,在HH/MM/SS
format.
let unix_timestamp = 1549312452;
// Create a new JavaScript Date object based on the timestamp
// multiplied by 1000 so that the argument is in milliseconds, not seconds
var date = new Date(unix_timestamp * 1000);
// Hours part from the timestamp
var hours = date.getHours();
// Minutes part from the timestamp
var minutes = "0" + date.getMinutes();
// Seconds part from the timestamp
var seconds = "0" + date.getSeconds();
// Will display time in 10:30:23 format
var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
console.log(formattedTime);
有关 Date 对象的更多信息,请参阅MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date or the ECMAScript 5 规范 http://ecma-international.org/ecma-262/5.1/#sec-15.9.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)