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中文網其他相關文章!