簡體   English   中英

將毫秒轉換為日期字符串

[英]Convert milliseconds to date string

我想將milliseconds轉換為string來表示時間量。

 function convertMsToDate(ms) { return ('' + Math.floor(ms / 86400000) + 'd -' + Math.floor((ms % 10) / 60).toString().padStart(2, '0')); } console.log(convertMsToDate(100000000));

const d = new Date(1238544816452);

d.toLocaleString(); // output: "5/25/2015, 1:34:06 PM"

d.toLocaleDateString(); // output: "7/25/2015"

d.toDateString();  // output: "Mon Jul 25 2015"

d.toTimeString(); // output: "13:35:07 GMT+0530 (India Standard Time)"

d.toLocaleTimeString(); // output: "1:35:07 PM"

我認為此代碼位於: Convert a Unix timestamp to time in JavaScript可以幫助您。

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);
const milliseconds = 1575909015000

const dateObject = new Date(milliseconds)

const humanDateFormat = dateObject.toLocaleString() //2019-12-9 10:30:15

dateObject.toLocaleString("en-US", {weekday: "long"}) // Monday
dateObject.toLocaleString("en-US", {month: "long"}) // December
dateObject.toLocaleString("en-US", {day: "numeric"}) // 9
dateObject.toLocaleString("en-US", {year: "numeric"}) // 2019
dateObject.toLocaleString("en-US", {hour: "numeric"}) // 10 AM
dateObject.toLocaleString("en-US", {minute: "numeric"}) // 30
dateObject.toLocaleString("en-US", {second: "numeric"}) // 15
dateObject.toLocaleString("en-US", {timeZoneName: "short"}) // 12/9/2019, 10:30:15 AM CST

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM