'How to convert result from Date.now() to yyyy/MM/dd hh:mm:ss ffff?

I'm looking for something like yyyy/MM/dd hh:mm:ss ffff

Date.now() returns the total of milliseconds (ex: 1431308705117).

How can I do this?



Solution 1:[1]

You can use the Date constructor which takes in a number of milliseconds and converts it to a JavaScript date:

var d = new Date(Date.now());
d.toString() // returns "Sun May 10 2015 19:50:08 GMT-0600 (MDT)"

In reality, however, doing Date(Date.now()) does the same thing as Date(), so you really only have to do this:

var d = new Date();
d.toString() // returns "Sun May 10 2015 19:50:08 GMT-0600 (MDT)"

Solution 2:[2]

You can use native JavaScript Date methods to achieve that or you can use a library like Moment.js.

It is a simple as:

moment().format('YYYY/MM/D hh:mm:ss SSS')

If you are going use a lot of date formatting/parsing in your application then I definitely recommend using it.

Solution 3:[3]

You can use Date().toISOString(), i.e.:

let d = new Date().toISOString();
alert(d);

Output:

2022-02-04T17:46:16.100Z 

Demo:

let d = new Date().toISOString();
document.write(d);

Solution 4:[4]

Simple

const DateNow = Date.now(); // 1602710690936
console.log(new Date(DateNow).toString()) // returns "Sun May 10 2015 19:50:08 GMT-0600 (MDT)"

Solution 5:[5]

function formatted_date()
{
   var result="";
   var d = new Date();
   result += d.getFullYear()+"/"+(d.getMonth()+1)+"/"+d.getDate() + 
             " "+ d.getHours()+":"+d.getMinutes()+":"+
             d.getSeconds()+" "+d.getMilliseconds();
   return result;
}

console.log(formatted_date())

Output: "2015/5/10 22:5:26 429"

Solution 6:[6]

function millisecondsToHuman(ms) {
    const seconds = Math.floor((ms / 1000) % 60);
    const minutes = Math.floor((ms / 1000 / 60) % 60);
    const hours = Math.floor(ms / 1000 / 60 / 60);

    const humanized = [
       pad(hours.toString(), 2),
       pad(minutes.toString(), 2),
       pad(seconds.toString(), 2),
    ].join(':');

return humanized;
}

function pad(numberString, size) {
   let padded = numberString;
   while (padded.length < size) padded = `0${padded}`;
   return padded;
}

Solution 7:[7]

Step 1: use new Date() to get the date as JavaScript format as Sun Jul 12 2020 15:40:16 GMT+0800 (Singapore Standard Time)

var d = new Date()

Step 2: use .toString() to convert to string and .substr string method to convert the previous string to "Jul 12 2020" and get rid of the rest

var d2 = d.toString().substr(4, 11)

Step 3: use .slice method to add '/' between dat, month and year to get Jul / 12 / 2020

var d3 = d2.slice(0, 3) + ' /' + d2.slice(3, 6) + ' /' + d2.slice(6))

Solution 8:[8]

var date = new Date();

will get you an answer formatted like this: Sun May 10 2015 21:55:01 GMT-0400 (Eastern Daylight Time)

var d = new Date(); var n = d.toJSON();

will get you the answer formatted the way you were looking for it.

Here is a great explanation of all the ways to manipulate the Date object

Solution 9:[9]

I like the dataformat package: you can install using: npm i dataformat.

and you can use like that:

dateFormat(medicao.DataHora, 'UTC:HH:MM')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3
Solution 4 Kabeer Jaffri
Solution 5 Steephen
Solution 6 Udhav Sarvaiya
Solution 7 Cosmin Staicu
Solution 8 JPRukus75
Solution 9 Vanderley Maia