The JSON string I want to convert contains date values of the format value = "YYYY-MM-DDTHH:MM:SSZ"
but for some technical reasons I won't digress into new Date(value)
won't work. However, a value of the format "YYYY/MM/DD HH:MM:SS"
will work.
Also, using another method, breaking value down into year, month, date, hour, minute, second and creating a new Date() with those values also works.
The seemingly longer approach, to break the value down into parts looks like this:
var ymd = value.split('T')[0].split('-');
var hms = value.split('T')[1].substr(0,8).split(':');
return new Date(ymd[0], ymd[1]-1, ymd[2], hms[0], hms[1], hms[2]);
The one-liner, simpler looking approach is this:
return new Date(value.replace(/-/g, '/').replace('T', ' ').substr(0,19));
Both work, but the first one, which looks more complicated, is noticeably faster than the one-liner. I have an Array with some hundreds of elements, each containing an Object having several date values in the original string format. With the first, longer-looking method everything returns basically instantly. With the second one-liner approach there is a noticeable pause of a second or two with each return.
I'm just curious why the first approach is so much faster than the second approach. Any ideas?
Thanks.