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!

3
  • Does this answer your question? How to format a JavaScript date? Commented Jul 15, 2021 at 18:41
  • AM/PM has nothing to do with time zone, just whether the time is before or after noon. Commented Jul 15, 2021 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!! Commented Jul 15, 2021 at 18:54

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much!
Because new Date().toLocaleString('en-US').replace(',', '') is not enough code? ;-)
1

Try this:

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

1 Comment

Thank you Shivam!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.