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 number of milliseconds (for example: 1431308705117).

How can i do this?

+8
source share
7 answers

You can use native JavaScript Date methods for this, or use a library like Moment.js .

It is just like:

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

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

+7
source

You can use the Date constructor, which takes a few milliseconds and converts it to a JavaScript date:

 var d = 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 need to do this:

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

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

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

Conclusion:

 2015-05-11T01:56:52.501Z 

Demonstration:

http://jsfiddle.net/dg8a16pz/

+3
source
 var date = new Date(); 

You will receive a response formatted as follows: Sun May 10, 2015 21:55:01 GMT-0400 (Eastern Daylight Time)

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

will provide you with an answer formatted as you searched for it.

Here is a detailed explanation of all ways to manipulate a Date object.

0
source
 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()) 

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

0
source
 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; } 
0
source
 var y=Date.now(); console.log((new Date(y)).toString()) 
0
source

All Articles