So I encounter a very weird problem here and am out of ideas at the moment.
I've the following function to parse and sort a json object by its property RowDate
, containing datestrings with the following format: "/Date(1389682861507+0100)/"
.
orderByDate: function (json) {
debug('[DataCollection::orderByDate], json before ordering: ', json);
_.each(json.LineDefinitions, function (line, i) {
json.LineDefinitions[i].Artifacts.sort(function (a, b) { //Artifacts is an Array of json subobjects containing "RowDate"
return MyApp.Utils.formatDate(a.RowDate) - MyApp.Utils.formatDate(b.RowDate);
});
});
debug('[DataCollect::orderByDate], json after ordering: ', json);
return json;
},
Here each RowDate
contains a string like the one given above (different dates, though).
The formatDate
-function looks like this:
MyApp.Utils.formatDate = function (date) {
var ret;
if (date instanceof Date) {
ret = '/Date(' + date.getTime()
+ (date.getTimezoneOffset() <= 0 ? '+' : '-')
+ String(('0000' + ((date.getTimezoneOffset() / 60) * -100)).substr(-4)) + ')/';
} else {
ret = new Date(Number(String(date).replace(/(^.*\()|([+-].*$)/g, '')));
}
return ret;
};
for my problematic case, the programflow uses the else
-condition.
Now it works out well in IE10 and 11(sic!) and Firefox 38.0.5, the resulting order is just right. But in chromes latest stable (you've heard right), v43.., i get the wrong ordering in my console.log, leading to unexpected behaviour later on in my app.
So I found this thread over here: Different values with Date() in Chrome and IE (being in germany here, localtime might be different, so I think this might be the reason?!)
and tried to change the formatDate-else-line into the following ones, using momentjs (came to this project later on. first thing i changed was using momentjs everywhere...):
ret = moment.utc(date).toDate();
=> no effect, so I tried this from the momentjs-docs, too:
ret = moment(date, moment.ISO_8601).toDate();
but this doesn't seem to do the trick too. At the moment I am out of ideas and I hope you people here could help me out with my question: How do I change chromes Date-parsing to match ff/ie parsing? Or do I have a real big knot in my brain and some other thing went wrong here?
Would be great to hear from you!
EDIT To make things clearer, I created this fiddle over here: http://jsfiddle.net/o3zh2kv4/8/
in IE and firefox, the output is:
test1, test8, test2, test9, test3, test10, test4, test11, test5, test12, test6, test13, test7, test14,
which is my desired output for further logic. but in chrome, i get the following:
test8, test1, test9, test2, test10, test3, test4, test11, test5, test12, test6, test13, test7, test14,
and I don't know why it is. Would be great to get some help here! I need chrome to change the order, too.