Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

For example, I have a string date like this (I'm getting this from the server in json, from rails app)

s = "2013-09-01T00:00:00.000+08:00"

I would like to display it like so

01.09.2013

So I'm using moment.js library for this

moment(s).zone("+08:00").format("DD.MM.YYYY")
>> "01.09.2013"

But I don't know if needed timezone is +08:00. If I skip .zone() call, result would be wrong because my browser is in differnt timezone

moment(s).format("DD.MM.YYYY")
>"31.08.2013"

Even though in my original string I had +08:00 at the end.

So, my question is how can I extract time zone from json date string using pure javascript or moment.js library?

The simplest way I can think of is extracting the last 6 characters manually,

s.slice(s.length - 6, s.length)
> "+08:00"

But maybe there is a better approach for this task?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
503 views
Welcome To Ask or Share your Answers For Others

1 Answer

Just use the parseZone function, like so:

moment.parseZone(s)

Documentation is here.

Alternatively, you can use the older approach, which does the same thing:

moment(s).zone(s)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...