2

I am trying to get specific format of datetime with time zone

i am getting string of time format which is shown below

var dateTime = "2020-06-01T01:50:57.000Z CDT"

I need to convert the format in to

 const offsetTime = moment(date).add("-0.00", 'hours')
 const formatedDate = moment(offsetTime, 'h:mm:ss A')
.utc()
.format('h:mm A')//(1:50 AM)

Required output

(1:50 AM CDT)

Do i need to split the string and get the format or do we have any method to convert it to this format in momentjs

In simple way to say

YYYY-MM-DDTHH:mm:ss.SSS[Z] z To hh:mm A z //format

and if the string contains only 2 character like "CT" instead of CDT how to capture that.

user11159070
  • 51
  • 1
  • 6
  • 1
    Use the time string you have to create a Date object, "new Date(your string);" then from the date object you have a lot of additional functionality that will allow you to do what you want: https://www.w3schools.com/js/js_dates.asp – SPlatten Jun 01 '20 at 08:41
  • is there any thing with momentjs?? – user11159070 Jun 01 '20 at 08:44
  • Does this answer your question? [Format datetime with moment.js to show timezone](https://stackoverflow.com/questions/31836765/format-datetime-with-moment-js-to-show-timezone) – Heretic Monkey Jun 03 '20 at 16:05

3 Answers3

3

You can zz to get timezone in output. For ex: moment()..format('h:mm A zz')

More documentation here momentJS

Ninad
  • 423
  • 2
  • 8
  • what i need is the string what i am getting is with timezone, so after format i need it to be something like this (1:50 AM CDT) – user11159070 Jun 01 '20 at 09:02
2

Use the moment-timezone to achieve this. Use the moment constructor to specify the input format, then specifying the required timezone. Finally use moment's format to get the required format

var dateTime = "2020-06-01T01:50:57.000Z CDT";
var timezone = "America/Chicago";
console.log(
  moment(dateTime, "YYYY-MM-DD hh:mm:ss zz")
.tz(timezone)
.format("h:mm A zz")
);
<script src="https://momentjs.com/downloads/moment.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data-2012-2022.min.js"></script>
joy08
  • 9,004
  • 8
  • 38
  • 73
1

Your date string is in ISO format with the 'Z' after seconds indicating that it is in UTC time. I am assuming that the 'CDT' is placed in the string in order to indicate which time zone this should be converted to. If you have control over how this string is represented then I recommend changing it so that you indicate the desired timezone elsewhere and simply store the date in UTC format. This way you can initialize a date or moment object with the ISO string as follows:

var date = moment("2020-06-01T01:50:57.000Z")

It is inconvenient the way it is currently since you cannot initialize it this way:

var date = moment("2020-06-01T01:50:57.000Z CDT")

The only option for handling the date in its current form is to parse it. You can do that like this:

var dateTime = "2020-06-01T01:50:57.000Z CDT"
var trimmed = dateTime.trim()  // remove leading and trailing whitespace
var isoString = trimmed.substr(0, trimmed.indexOf(' '))

Which will produce the following string

2020-06-01T01:50:57.000Z

You can use that string I called "isoString" to initialize a date or moment object. The next obstacle is to handle converting that UTC string to a certain timezone (in this case CDT). It is simple if you want to convert the UTC date to the current users timezone since that will happen automatically when you initialize the moment or date object with the ISO date string. Otherwise, you need some way to get the timezone from 'CDT' into the format moment wants which was shown by @vjr12 ("America/Chicago"). The only way to do this is to either store that with the date string or create a mapping. It is much easier to convert from "America/Chicago" to "CDT" than it is to convert from "CDT" to "America/Chicago". Your only option with the current form is to create your own mapping from "CDT" to "America/Chicago". You could do something like:

let tzMap = new Map()
tzMap.set('CDT','America/Chicago')
// Set the rest of your timezones

You would need to do that for all timezones and then you could use the timezone parsed from your date string like this:

var tzAbbr = trimmed.substr(trimmed.indexOf(' ') + 1)

which will grab the "CDT" or "CT" for that matter. Then you could use your mapping like this:

var timezone = tzMap.get(tzAbbr)

timezone will be "America/Chicago" in this case and then you can use @vjr12 solution from here to get the form you want.

Note

I highly recommend that (if you are able) to change the current format of the datestring that you are using. The purpose of using UTC time is to be timezone agnostic so it does not make sense to store the timezone with the UTC string. If you want to preserve the timezone then you would be better off using a format which already embeds the timezone.

Grant Singleton
  • 1,611
  • 6
  • 17