Unix 时间戳用于表示 MySQL 数据库中的时间值,有时需要仅提取特定应用程序的时间分量。要将 Unix 时间戳转换为 JavaScript 中的时间,请按照以下步骤操作:
创建 JavaScript 日期对象:
let unix_timestamp = 1549312452; let date = new Date(unix_timestamp * 1000); // Convert to milliseconds
提取时间组件:
// Hours: const hours = date.getHours(); // Minutes: const minutes = "0" + date.getMinutes(); // Pad zero if necessary // Seconds: const seconds = "0" + date.getSeconds(); // Pad zero if necessary
格式化时间:
const formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
const unix_timestamp = 1549312452; // Create a JavaScript Date object based on the timestamp // multiplied by 1000 so that the argument is in milliseconds, not seconds const date = new Date(unix_timestamp * 1000); // Hours part from the timestamp const hours = date.getHours(); // Minutes part from the timestamp const minutes = "0" + date.getMinutes(); // Seconds part from the timestamp const seconds = "0" + date.getSeconds(); // Will display time in 10:30:23 format const formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2); console.log(formattedTime);
以上是如何在 JavaScript 中从 Unix 时间戳中提取时间?的详细内容。更多信息请关注PHP中文网其他相关文章!