1

I am trying to convert a time string returned from our backend system but I am unable to get the correct local time.

Time string returned from backend: "2020-08-28T07:00:00.000Z"

Expected local time needed: Aug 28, 2020 12:00 AM

I tried:

  1. converting it with Moment.js Failed:

        moment(backendTime).format(
         "MMM D,YYYY h:mm a"
      ) 
    

    Somehow I got "Jan 1st 20 12:00 AM", I don't understand it at all

  2. converting it by casting to Date Failed as well:

  Date("2020-08-28T07:00:00.000Z")

Ended up getting: " Mon Sep 21 2020 00:07:29 GMT-0700 (Pacific Daylight Time) "

I am running out of ideas right now, I feel it shouldn't be this difficult but just cannot get it.

Cninroh
  • 1,796
  • 2
  • 21
  • 37
Edward Sun
  • 53
  • 5

3 Answers3

0

The format you are getting is exactly what you have requested.

To get the output you desire using moment.js you need to use the following format:

moment(backendTime).format("MMM DD, YYYY hh:mm A");

You can test it here: https://jsfiddle.net/oz6mx3nh/

Cninroh
  • 1,796
  • 2
  • 21
  • 37
0

In vanilla JS you can use the DateTimeFormat to format your dates based on a timezone. All the options for DateTimeFormat are documented here.

The different options for the locale (en-US) is listed here

Here is a full list of all the time zones.

See bottom snippet for the output of each locale (in the provided list)

const date = new Date("2020-08-28T07:00:00.000Z");

const format = new Intl.DateTimeFormat(
  "en-US",
  {
    month: "short",
    day: "2-digit",
    year: "numeric",
    hour: "2-digit",
    hour12: true,
    minute: "2-digit",
    timeZone: "America/New_York",
  }
).format(date)

console.log(format);

A list of outputs from every locale:

const date = new Date("2020-08-28T07:00:00.000Z");

const locales = ["ar-SA", "bn-BD", "bn-IN", "cs-CZ", "da-DK", "de-AT", "de-CH", "de-DE", "el-GR", "en-AU", "en-CA", "en-GB", "en-IE", "en-IN", "en-NZ", "en-US", "en-ZA", "es-AR", "es-CL", "es-CO", "es-ES", "es-MX", "es-US", "fi-FI", "fr-BE", "fr-CA", "fr-CH", "fr-FR", "he-IL", "hi-IN", "hu-HU", "id-ID", "it-CH", "it-IT", "jp-JP", "ko-KR", "nl-BE", "nl-NL", "no-NO", "pl-PL", "pt-BR", "pt-PT", "ro-RO", "ru-RU", "sk-SK", "sv-SE", "ta-IN", "ta-LK", "th-TH", "tr-TR", "zh-CN", "zh-HK", "zh-TW"];

const formatDate = (locale, date) => new Intl.DateTimeFormat(
  locale,
  {
    month: "short",
    day: "2-digit",
    year: "numeric",
    hour: "2-digit",
    hour12: true,
    minute: "2-digit",
    timeZone: "America/New_York",
  }
).format(date);

// Loop over each locale
for(const locale of locales) {
  const formatted = formatDate(locale, date);
  
  console.log(`${locale}: ${formatted}`);
}
Reyno
  • 6,119
  • 18
  • 27
0

A few things:

  • Assuming your computer is set for Pacific time, then:

    moment("2020-08-28T07:00:00.000Z").format("MMM D, YYYY h:mm a")
    //=> "Aug 28, 2020 12:00 am"
    

    That will indeed return the value you expected. You said you got "Jan 1st 20 12:00 AM", but that is not possible, as neither the values nor the format matches.

  • When you tried the Date object, you got the result you did because you omitted the new keyword (see this explanation). It should instead be:

    new Date("2020-08-28T07:00:00.000Z")
    

    Keep in mind that this produces a Date object. If you log that object directly, the result is implementation specific. It may vary across browsers. Instead, you should call a function such as toString or toLocaleString from it.

  • If this is a new project, you should probably avoid using Moment. Please read https://momentjs.com/docs/#/-project-status/

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575