Why is JavaScript showing me the day before my set date?

I am trying to see dates based on some json data that I have.

My code is:

var date = new Date(json.events[i].event.first_date); alert(date); 

Now that json.events[i].event.first_date just returns a date in the format yyyy-mm-dd .

I noticed, however, that when I do alert(date); They show me the day before the date that the actual data is talking about.

For example, json.events[0].event.first_date indicates the date 2015-06-02 , but the warning is displayed on June 1, 2015.

I get my json from a URL based somewhere in Germany and I am in the USA. Can a date be ruined due to time zones?

+7
javascript date timezone
source share
1 answer

When you create a date from a string without a time zone, you get a date + time zone adjustment - if you are in the USA, then you have something like GMT-7, and you get the second of June minus 7 hours - the previous day. Try to split the date and use the new Date(2015, 7, 1) constructor, and you will get the date you expect. String parse reference docs - https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

+3
source share

All Articles