1

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!

audreywrong
  • 101
  • 9
  • Does this answer your question? [How to format a JavaScript date?](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – Heretic Monkey Jul 15 '21 at 18:41
  • AM/PM has nothing to do with time zone, just whether the time is before or after noon. – Heretic Monkey Jul 15 '21 at 18:42
  • @HereticMonkey - that post is definitely helpful for being able to see a high level overview of all of the options - I really appreciate that!! – audreywrong Jul 15 '21 at 18:54

2 Answers2

2

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

    console.log(dformat);
J nui
  • 188
  • 1
  • 13
1

Try this:

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