0

I have a string:

var utc_datetime = "02/09/2018 1:27 a.m."

I need it to show up in the user's local time. The format will always be the same, and it will always be queried in UTC timezone.

How can I accomplish this? I tried moment, and moment-timezone, but no luck so far.

1
  • Please don't add the answer to the question. If you feel the answer given could be improved, edit it. Commented Mar 1, 2018 at 0:47

1 Answer 1

1

Remove the dots and capitalize the AM or PM: Append 'UTC' to the string then convert it to a date

var utc_datetime = "02/09/2018 1:27 a.m."
var new_datestring = utc_datetime.replace(/\./g,'').toUpperCase() + ' UTC';
var date = new Date(new_datestring);
var local_datetime = date.toString();
console.log(local_datetime)

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

2 Comments

new_datestring = utc_datetime + ' UTC' var date = new Date(new_datestring) winds up spitting out "Invalid Date"
This just needed one very small tweak because the a.m. needed to be AM :) I'll add the solution in the problem

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.