Convert yyyy-MM-ddTHH:mm:ss.fffZ to DateTime in Ja

2019-02-09 06:29发布

问题:

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?

回答1:

Here's a one liner from John Resig:

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


回答2:

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));
}


回答3:

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)


回答4:

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"}