how to convert milliseconds time in javascript in javascript

To convert milliseconds to a date/time string in JavaScript, you can use the Date constructor and its methods. Here is an example function that takes a milliseconds parameter and returns a formatted date/time string:

index.tsx
function formatDate(milliseconds) {
  const date = new Date(milliseconds);
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hours = date.getHours();
  const minutes = date.getMinutes();
  const seconds = date.getSeconds();
  return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')} ${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
}
493 chars
11 lines

You can call this function with a milliseconds value to get a formatted string representing the date and time:

index.tsx
const milliseconds = 1609459200000; // January 1, 2021 00:00:00 UTC
const formatted = formatDate(milliseconds);
console.log(formatted); // "2021-01-01 00:00:00"
161 chars
4 lines

gistlibby LogSnag