Conversion between ISO 8601 date extended format and UTC time type

Source: Internet
Author: User
Tags iso 8601 iso 8601 date

ISO 8601 extended format YYYY-MM-DDTHH: mm: ss. sssZ example: (section circled with a red border)


Note: This format is supported only when ECMAScript 5 is compatible. Browsers that support this time format include IE9, Chrome, FireFox, Safari, etc. IE browser before IE9 does not support this time format.

To convert an ISO 8601 time type to a UTC time type, follow these steps (compatible and not compatible with ECMAScript 5 browsers ):

[Javascript]
Date. prototype. setISO8601 = function (string ){
Var regexp = "([0-9] {4}) (-([0-9] {2}) (-([0-9] {2})" +
"(T ([0-9] {2}) :( [0-9] {2}) (:( [0-9] {2 })(\. ([0-9] + ))?)? "+
"(Z | ([-+]) ([0-9] {2}) :( [0-9] {2 })))?)?)?)? ";
If (string)
{
Var d = string. match (new RegExp (regexp ));
Var offset = 0;
Var date = new Date (d [1], 0, 1 );
 
If (d [3]) {
Date. setMonth (d [3]-1 );
}
If (d [5]) {
Date. setDate (d [5]);
}
If (d [7]) {
Date. setHours (d [7]);
}
If (d [8]) {
Date. setMinutes (d [8]);
}
If (d [10]) {
Date. setSeconds (d [10]);
}
If (d [12]) {
Date. setMilliseconds (Number ("0." + d [12]) * 1000 );
}
If (d [14]) {
Offset = (Number (d [16]) * 60) + Number (d [17]);
Offset * = (d [15] = '-')? 1:-1 );
}
Offset-= date. getTimezoneOffset ();
Time = (Number (date) + (offset x 60x1000 ));
This. setTime (Number (time ));
}
Else
{
Return;
}
}
The reference method is as follows:
[Html]
Var today = new Date ();
Today. setISO8601 (temprature_time_sorted_uniq [I]. tm );
Temprature_time_sorted_uniq [I]. tm is the time parameter of the ISO 8601 type passed to the function. The value is as follows:

 

The converted value is:

 

To convert the UTC format to ISO 8601

[Html]
Function ISODateString (d ){
Function pad (n ){
Return n <10? '0' + n: n
}
Return d. getUTCFullYear () + '-'
+ Pad (d. getUTCMonth () + 1) + '-'
+ Pad (d. getUTCDate () + 'T'
+ Pad (d. getUTCHours () + ':'
+ Pad (d. getUTCMinutes () + ':'
+ Pad (d. getUTCSeconds () + 'Z'
}
Reference Method:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.