0

I want to convert milliseconds to a string to represent the amount of time.

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

console.log(convertMsToDate(100000000));
mrks
  • 4,642
  • 7
  • 38
  • 56
  • You can create a date object with milliseconds. Then use the date object to format. Does this help? => https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date – daddygames Aug 21 '20 at 12:35
  • Does this answer your question? [Converting milliseconds to a date (jQuery/JavaScript)](https://stackoverflow.com/questions/4673527/converting-milliseconds-to-a-date-jquery-javascript) – Heretic Monkey Aug 21 '20 at 12:42
  • See also [Javascript show milliseconds as days:hours:mins without seconds](https://stackoverflow.com/q/8528382/215552) – Heretic Monkey Aug 21 '20 at 12:52
  • Do you mean the elapsed amount of time or just convert the ms to a date? – Argee Aug 21 '20 at 12:54
  • The title and the body of the question say two different things. – Heretic Monkey Aug 21 '20 at 12:56
  • Your last edit return a very different result from your original post. https://jsfiddle.net/obpyr4w0/1/ will return a similar format to your original – Martheen Aug 21 '20 at 14:08

3 Answers3

1
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"
1

I think this code found at: Convert a Unix timestamp to time in JavaScript can help you.

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);
  • Note that first comment; Unix timestamps are in seconds, not milliseconds, so this code actually would convert the milliseconds the OP has to nanoseconds, producing a very odd date indeed. – Heretic Monkey Aug 21 '20 at 12:50
1
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