audreywrong
audreywrong

Reputation: 111

JavaScript Date/Time Formatting

I am needing to convert the standard Date(); JS object to the following format: 7/13/2021 8:47:58 PM (M/d/yyyy HH:mm:ss Z)

I'm struggling to get this EXACT format in the simplest way possible. This is what I have so far:

var d = new Date,
    dformat = [d.getMonth()+1,
               d.getDate(),
               d.getFullYear()].join('/')+' '+
              [d.getHours(),
               d.getMinutes(),
               d.getSeconds()].join(':');

console.log(dformat);

I am struggling to get the time zone, which I am conjecturing adds the AM/PM (I could very well be mistaken).

Any help is greatly appreciated!

Upvotes: 1

Views: 324

Answers (2)

J nui
J nui

Reputation: 190

var d = new Date,
        dformat = [d.getMonth()+1,
                   d.getDate(),
                   d.getFullYear()].join('/')+' '+
                  d.toLocaleTimeString('en-US')

    console.log(dformat);

Upvotes: 2

Shivam Gupta
Shivam Gupta

Reputation: 26

Try this:

var d = new Date();
console.log(d.toLocaleString());

Upvotes: 1

Related Questions