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

Convert yyyy-MM-ddTHH:mm:ss.fffZ to DateTime in JavaScript manually - Stack Overflow

programmeradmin4浏览0评论

I receive from a Webservice a String with a date in this format:

yyyy-MM-ddTHH:mm:ss.fffZ

I need to convert that String with JavaScript to a normal DateTime but without using the new Date('yyyy-MM-ddTHH:mm:ss.fffZ') because I'm using an old version of JavaScript that not support that conversion. I can split that string and get the:

  • Year
  • Month
  • Days
  • Time

but how to manipulate the time zone "fffZ" Any suggestions?

I receive from a Webservice a String with a date in this format:

yyyy-MM-ddTHH:mm:ss.fffZ

I need to convert that String with JavaScript to a normal DateTime but without using the new Date('yyyy-MM-ddTHH:mm:ss.fffZ') because I'm using an old version of JavaScript that not support that conversion. I can split that string and get the:

  • Year
  • Month
  • Days
  • Time

but how to manipulate the time zone "fffZ" Any suggestions?

Share Improve this question edited Jan 6, 2015 at 0:32 Marcello B. 4,44011 gold badges46 silver badges69 bronze badges asked Jan 9, 2013 at 15:08 MartínMartín 3,1258 gold badges27 silver badges43 bronze badges 4
  • 1 What is your javascript environment? – Aaron Kurtzhals Commented Jan 9, 2013 at 15:11
  • 3 fffZ is not a time zone - the time zone is Z (Zulu time, which means UTC), and fff are three decimals that belong to the seconds part (ss.fff) - if you take all three together, they form the milliseconds part. – MvanGeest Commented Jan 9, 2013 at 15:36
  • Android 2.3 (Using PhoneGap and ChildBrowser) – Martín Commented Jan 9, 2013 at 15:56
  • This is my real String 2013-01-08T17:16:36.000Z – Martín Commented Jan 9, 2013 at 16:00
Add a comment  | 

4 Answers 4

Reset to default 10

Here's a one liner from John Resig:

var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),

I've founded the solution. Please check http://webcloud.se/log/JavaScript-and-ISO-8601/

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})))?)?)?)?";
    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 * 60 * 1000));
    this.setTime(Number(time));
}

If you know it will be of this form (ISO 8601, wiki), you can parse with RegExp or string methods. Here is a RegExp example that lets you use timezone Z, +hh or +hh:mm.

var dateString = '2013-01-08T17:16:36.000Z';

var ISO_8601_re = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{3}))?(Z|[\+-]\d{2}(?::\d{2})?)$/,
    m = dateString .match(ISO_8601_re);

var year = +m[1],
    month = +m[2],
    dayOfMonth = +m[3],
    hour = +m[4],
    minute = +m[5],
    second = +m[6],
    ms = +m[7], // +'' === 0
    timezone = m[8];

if (timezone === 'Z') timezone = 0;
else timezone = timezone.split(':'), timezone = +(timezone[0][0]+'1') * (60*(+timezone[0].slice(1)) + (+timezone[1] || 0));
// timezone is now minutes

// your prefered way to construct
var myDate = new Date();
myDate.setUTCFullYear(year);
myDate.setUTCMonth(month - 1);
myDate.setUTCDate(dayOfMonth);
myDate.setUTCHours(hour);
myDate.setUTCMinutes(minute + timezone); // timezone offset set here, after hours
myDate.setUTCSeconds(second);
myDate.setUTCMilliseconds(ms);

console.log(myDate); // Tue Jan 08 2013 17:16:36 GMT+0000 (GMT Standard Time)

momentjs has the answer to this and many other date problems you might have. While it isn't clear where and how you will user the needed date, neither the wanted format, I think momentjs can give you some of the needed tasks I would add the module to my solution and use as (below is parse.com cloud code):

Parse.Cloud.define("momentFormat", function(request, response){
    var message;

    var date = momento('2013-01-08T17:16:36.000Z');
    response.success("original format date: " + date.format("YYYY-MM-DDTHH:mm:ss.SSSZ") + " new format date: " + date.format("dddd, MMMM Do YYYY, h:mm:ss a"));
});

Output:

{"result":"original format date: 2013-01-08T17:16:36.000+00:00 new format date: Tuesday, January 8th 2013, 5:16:36 pm"}
发布评论

评论列表(0)

  1. 暂无评论