0

I have a DateTime that I get from an API that looks like this 2019-06-19T21:17:08.915Z I believe it is called ISO 8601 but it does not have a user-friendly appearance. I want display it as formatted local date time.

For example, my current timezone looks like this 5:20 PM Wednesday, June 19, 2019 (EDT)

I tried to make something look similar to the one in the example.

I am using JavaScript and Vue.js. Is there a function I can use in the library to convert this DateTime.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Xuan
  • 51
  • 1
  • 3
  • 7

2 Answers2

1

You can't manually set a timezone (see here) - but this will automatically adjust to whichever timezone you're in:

console.log(new Date("2019-06-19T21:17:08.915Z"));

(doesn't produce correct output on SO)

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • Are you following me? – Choc13 Jun 19 '19 at 21:34
  • Yes @Choc13 - your answer doesn't produce the OP's exact required output, but it's the method I would have gone with. – Jack Bashford Jun 19 '19 at 21:35
  • You can set the timezone for a timestamp using [*toLocaleString*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString) with the timezone option, or one of many libraries. – RobG Jun 20 '19 at 02:22
0

Does new Date("2019-06-19T21:17:08.915Z").toLocaleString() not do the trick?

Choc13
  • 796
  • 7
  • 23
  • The output of *toLocaleString* is implementation dependent and may or may not include the local timezone, and may or may not include the timezone name. – RobG Jun 20 '19 at 08:37