admin管理员组文章数量:1319023
I am using moment.js
, and I want to convert from ISO Format date time to UTC time zone.
I can convert from local time to UTC+ISOformat, but I am not able to convert ISO format to UTC time zone.
Input:
2018-03-22T00:00:00Z
Expected output:
date should be in UTC time zone. If I calculate the it should be:
22018-03-21T18:30:00Z
- First I want to convert into ISO, After that convert into UTC**.
Not able to Converted local date time to ISO then UTC
We can convert into string, But from ISO format can convert or not?
Fox example: I want to convert ISODATE(2018-03-22T00:00:00Z)
into UTC time zone.
function toISO(dt) {
return moment(dt).format("YYYY-MM-DDTHH:mm:ss") + "Z";
}
var date = new Date();
var isoDate= toISO(date)
Direct we can convert
function toISOWithUtc(dt) {
return moment(dt).utc().format("YYYY-MM-DDTHH:mm:ss") + "Z";
}
var date = new Date();
toISO(date)
I am using moment.js
, and I want to convert from ISO Format date time to UTC time zone.
I can convert from local time to UTC+ISOformat, but I am not able to convert ISO format to UTC time zone.
Input:
2018-03-22T00:00:00Z
Expected output:
date should be in UTC time zone. If I calculate the it should be:
22018-03-21T18:30:00Z
- First I want to convert into ISO, After that convert into UTC**.
Not able to Converted local date time to ISO then UTC
We can convert into string, But from ISO format can convert or not?
Fox example: I want to convert ISODATE(2018-03-22T00:00:00Z)
into UTC time zone.
function toISO(dt) {
return moment(dt).format("YYYY-MM-DDTHH:mm:ss") + "Z";
}
var date = new Date();
var isoDate= toISO(date)
Direct we can convert
function toISOWithUtc(dt) {
return moment(dt).utc().format("YYYY-MM-DDTHH:mm:ss") + "Z";
}
var date = new Date();
toISO(date)
Share
Improve this question
edited Mar 22, 2018 at 11:58
Brett DeWoody
62.9k31 gold badges144 silver badges192 bronze badges
asked Mar 22, 2018 at 10:34
Varun SharmaVarun Sharma
4,84213 gold badges55 silver badges105 bronze badges
5
- Please include inputs and expected outputs for each input. – Igor Commented Mar 22, 2018 at 10:38
- @Igor convert into UTC.. 2018-03-22T00:00:00Z – Varun Sharma Commented Mar 22, 2018 at 10:39
- 1 Let me rephrase: edit your question, include inputs and corresponding expected outputs. If you really want to be helpful to those looking at the question do it as if you were writing unit tests. – Igor Commented Mar 22, 2018 at 10:40
-
1
How is
2018-03-22T00:00:00Z
not in "date should be in UTC time zone"? Again, write it as if you were writing a unit test. What is the type you are checking and what should its value be? At the moment it is still very unclear what you are actually asking/needing. – Igor Commented Mar 22, 2018 at 10:50 -
2018-03-22T00:00:00Z
is not a local time, theZ
at the end means the timezone is UTC. – phuzi Commented Mar 22, 2018 at 12:03
1 Answer
Reset to default 5Both 2018-03-22T00:00:00Z
and 2018-03-21T18:30:00Z
already are in UTC.
Note the Z
in the end? This means that the date is in UTC. It's not a hardcoded letter that you can just append to the end - it has a specific meaning, it tells that the date/time is in UTC.
If you want to convert that UTC date to another timezone (I'm guessing that's what you want), you can use moment.tz:
// convert UTC 2018-03-22T00:00:00Z to Asia/Kolkata timezone
moment.tz('2018-03-22T00:00:00Z','Asia/Kolkata')
Just change the timezone name to the one you want to convert.
And calling format()
will give you the converted date/time in ISO8601 format:
moment.tz('2018-03-22T00:00:00Z','Asia/Kolkata').format() // 2018-03-22T05:30:00+05:30
Note that the offset changed from Z
to +05:30
. This means that the date/time above is 5 hours and 30 minutes ahead UTC, which is the result of converting 2018-03-22T00:00:00Z
to Kolkata's timezone.
Note: I think you're mistaking the concepts here.
UTC is a time standard. It defines things like the current date/time accross the world, it uses atomic clocks to make sure everybody on Earth are synchronized, and timezones are all based on offsets from UTC.
ISO8601 is a standard for exchanging date/time-related data. It defines formats (text representations of dates). A date/time can be in ISO format, regardless of being in UTC or not.
That's why "convert from UTC to ISO" (or vice-versa) makes no sense at all.
本文标签: javascriptHow to convert date time from ISO Format to UTC time zoneStack Overflow
版权声明:本文标题:javascript - How to convert date time from ISO Format to UTC time zone? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742050704a2418049.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论