0

I am attempting to convert the following into a 12 hour am/pm format.

Currently I am recieving the Day, Month, Year and timezone.

Fixed by adding .toLocaleTimeString().replace(/([\d]+:[\d]{2})(:[\d]{2})(.)/, "$1$3")*

<div id="time1"></div>
<div id="time2"></div>
var date = new Date('08/16/2019 12:00:00 PM UTC').toLocaleTimeString().replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3")
document.getElementById("time1").innerHTML = date;

var date = new Date('08/16/2019 6:00:00 am UTC').toLocaleTimeString().replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3")
document.getElementById("time2").innerHTML = date;

Jak
  • 31
  • 3
  • Possible duplicate of [Convert UTC date time to local date time](https://stackoverflow.com/questions/6525538/convert-utc-date-time-to-local-date-time) – computercarguy Aug 16 '19 at 18:43
  • @MattJohnson-Pint Updated to reflect the change – Jak Aug 16 '19 at 19:54
  • So, `"12pm"` the exact input you need converted, and that should be interpreted as 12:00 PM on the current UTC date? Have you tried anything to solve *that* yet? And for output, what are you looking for there? A `Date` object? A string in a specific format? Also, your comment about "does not work in the CDN I am using" is very unclear. There is no requirement to use a CDN to use a library like Moment, and if you are using jQuery then you are using libraries already. – Matt Johnson-Pint Aug 16 '19 at 20:00
  • @MattJohnson-Pint Updated to reflect your questions. The CDN = Wordpress seems to be conflicting with Moment.js – Jak Aug 16 '19 at 20:32

2 Answers2

1

Basically what you have to do is use the Date() default javascript function and make sure you append the UTC timezone:

var date = new Date('08/16/2019 7:00:00 PM UTC')

date.toString=() //will then print out the timezone adjusted time

"Fri Aug 16 2019 22:00:00 GMT+0300 (Eastern European Summer Time)"
AndreiS
  • 84
  • 2
  • Thank you Andrei, how would I then convert date.toString to display without dd/mm/yyyy, in 12 hour am/pm, without (timezone)? – Jak Aug 16 '19 at 19:17
0

There are many built in javascript methods to handle converting date objects. This example will look to the browser to determine date format and time.

let time = Date.now();
time.toLocaleDateString();
Jay Jordan
  • 643
  • 4
  • 16