Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 403
RollRoll Avatar asked May 11 '15 01:05

RollRoll


People also ask

How do I change a date to a date today?

In this article, we will learn about the Date now() method in Javascript. The date. now() method is used to return the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC. Since now() is a static method of Date, it will always be used as Date.

How do you find the date in YYYY-MM-DD format in moment?

const dateA = moment('01-01-1900', 'DD-MM-YYYY'); const dateB = moment('01-01-2000', 'DD-MM-YYYY'); console.

How do I convert a DateTime to a specific format?

Simply just do in this way. string yourFormat = DateTime. Now. ToString("yyyyMMdd");

How do you convert Toisostring to dates?

Use the Date() constructor to convert an ISO string to a date object, e.g. new Date('2023-07-21T09:35:31.820Z') . The Date() constructor will easily parse the ISO 8601 string and will return a Date object. Copied! We used the Date() constructor to create a Date object from an ISO string.


4 Answers

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)" 
like image 91
Sumner Evans Avatar answered Sep 25 '22 06:09

Sumner Evans


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.

like image 39
Luis Avatar answered Sep 23 '22 06:09

Luis


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);
like image 26
Pedro Lobito Avatar answered Sep 23 '22 06:09

Pedro Lobito


Simple

const DateNow = Date.now(); // 1602710690936
console.log(new Date(DateNow).toString()) // returns "Sun May 10 2015 19:50:08 GMT-0600 (MDT)"
like image 45
Kabeer Jaffri Avatar answered Sep 24 '22 06:09

Kabeer Jaffri