javascript - toISOString() changes datetime value -
i have array of objects.each object in array has date property.i try biggest(the last) date array.
here array:
var sensorsdata = [{ id: 1, measuredate: "2017-08-20t09:52:32" }, { id: 2, measuredate: "2017-08-20t09:54:35" }, { id: 3, measuredate: "2017-08-20t09:56:13" }];
and here function fetch biggest date array above:
function updatelatestdate(sensorsdata) { return new date(math.max.apply(null, sensorsdata.map(function(e) { return new date(e.measuredate); }))).toisostring(); }
the result updatelatestdate
function is:
2017-08-20t06:56:13.000z
but strange because, can see no 1 of properties in sensorsdata objects doesn't have date returned updatelatestdate
function.
here fiddler.
any idea why updatelatestdate function returns wrong result?
when create date new date(str)
creates date object time zone. toisostring()
makes 0 utc offset, denoted suffix "z".
here workaround:
var date = new date(e.measuredate) return new date(date.gettime() - date.gettimezoneoffset() * 60000)
updated fiddler: https://jsfiddle.net/xf5jmll6/7
Comments
Post a Comment