最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add time zone to specific format in momentjs? - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Jun 1, 2020 at 11:25 user11159070 asked Jun 1, 2020 at 8:39 user11159070user11159070 511 silver badge6 bronze badges 3
  • 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: w3schools./js/js_dates.asp – SPlatten Commented Jun 1, 2020 at 8:41
  • is there any thing with momentjs?? – user11159070 Commented Jun 1, 2020 at 8:44
  • Does this answer your question? Format datetime with moment.js to show timezone – Heretic Monkey Commented Jun 3, 2020 at 16:05
Add a ment  | 

3 Answers 3

Reset to default 4

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

More documentation here momentJS

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./downloads/moment.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data-2012-2022.min.js"></script>

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 remend 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 remend 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.

发布评论

评论列表(0)

  1. 暂无评论