1

Some of my users report different outputs, offset by one day.

Code:

var call=[1451084400000,1451170800000,1451257200000,1451343600000,1451430000000,1451516400000,1451602800000,1451689200000,1451775600000]

    var arrayLength = call.length;
    for (var i = 0; i < arrayLength; i++) {
    document.getElementById('output').innerHTML += moment(call[i],"x").format("YYYY-MM-DD")+"<br>"
    }

Expected result:

2015-12-26
2015-12-27
2015-12-28
2015-12-29
2015-12-30
2015-12-31
2016-01-01
2016-01-02
2016-01-03

Issue:

2015-12-25
2015-12-26
2015-12-27
2015-12-28
2015-12-29
2015-12-30
2015-12-31
2016-01-01
2016-01-02

Any ideas why?

meso_2600
  • 1,940
  • 5
  • 25
  • 50
  • More than likely related to timezone offsets. If you can log the actual millisecond count, you would more than likely see that the values are the same, you are just hitting day boundaries based on timezone. – ArcSine May 02 '16 at 22:57
  • Not an exact duplicate (it doesn't use moment.js) but it's the same issue: [*Why is new Date() removing a day?*](http://stackoverflow.com/questions/36698286/why-is-new-date-removing-a-day-javascript). – RobG May 02 '16 at 22:58
  • 2
    See also http://stackoverflow.com/questions/36165182/strange-behavior-formatting-moment-js-date/36168554#36168554. This is a moment specific question/answer. Basically, you want moment.utc() – Maggie Pint May 02 '16 at 23:04
  • @MaggiePint ok now when I use the parseZone I get the same issue my users see. But...the correct date is without the parseZone :) – meso_2600 May 02 '16 at 23:09
  • It sounds like you want the date to display in your time zone, even if the user is in a different time zone. You will need moment timezone for that. – Maggie Pint May 02 '16 at 23:11
  • Why don't you just pass a string? A time value represents an instant in time, so what the user sees will depend on the time zone setting of their host. If you just want to represent a date without regard for time zone, just send a string. – RobG May 02 '16 at 23:19
  • @RobG I pass this as integers as I need to do some sorting first. – meso_2600 May 02 '16 at 23:23
  • @RobG is correct in that, if you have a string, that's going to be way easier than moment timezone. Just make the string in ISO8601 format. That format sorts just fine in lexicographical order. – Maggie Pint May 02 '16 at 23:29
  • ok I have tried saving dates with .utc and .parseZone but this till occurs when I try to show the dates using .utc .parseZone (now all the dates are -1 day...) – meso_2600 May 02 '16 at 23:32
  • ok so assuming I get all the dates as integers via API, all set as unix timestamp I'd like to show them always in UTC. When I use .utc I get -1 day. that's the issue now – meso_2600 May 02 '16 at 23:36
  • 1
    What you are seeing is the correct date, you're assuming the wrong date. A time value of `1451084400000` is 2015-12-25T23:00:00.000Z, which is GMT. Change the time zone to 1 hour east or more and the date changes. – RobG May 03 '16 at 00:22
  • @RobG strings helped. This should be the answer posted by you – meso_2600 May 03 '16 at 00:47
  • @MaggiePint your link helped a lot! – meso_2600 May 03 '16 at 00:47
  • @RobG how do I mark your answer as answer to this question? – meso_2600 May 14 '16 at 21:57

1 Answers1

0

Try to convert to the desired timezone. For London timezone should be like this:

date.clone().tz("Europe/London");
Tiago Bértolo
  • 3,874
  • 3
  • 35
  • 53