Sterling Archer
Sterling Archer

Reputation: 22395

Date Format Issues

So I have this date returned from an API:

"2014-08-07T00:00Z"

And the results from new Date("2014-08-07T00:00Z") equal Wed Aug 06 2014 20:00:00 GMT-0400 (EDT) and .getDay() on that date gives me 3.

Why is it going from August 7th, to 6th, and the getDay returns 3?

Basically I'm trying to turn the API return date into english.

days[d.getDay()]+", "+months[d.getMonth()]+" "+getOrdinal(d.getDay())+" "+formatAMPM(d) 

(aka "Wednesday, August 3rd 8:00 pm")

console.log(d,data[i].startDate, d.getDay());
//yields
// Wed Aug 06 2014 20:00:00 GMT-0400 (EDT) "2014-08-07T00:00Z" 3

days is just an array of text days, as is months, getOrdinal is a function that gives the text st or nd or rd on the day, and formatAMPM is pretty obvious.

Upvotes: 1

Views: 52

Answers (2)

255kb - Mockoon
255kb - Mockoon

Reputation: 6974

I don't think that there is any problem here. Your date is 2014-08-07T00:00Z, with Z meaning Zulu Time Zone (equivalent to UTC), and using new Date() on it will convert to you local time, here GMT-4, that is why you are getting 4 hours difference. And For the "3", the getDay() method returns the day of the week, wednesday in your case.

Upvotes: 1

Alex Mcp
Alex Mcp

Reputation: 19315

.getDay() returns the day of the week. I think you'd expect .getDate()

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDate

That's why you're seeing that it is a Wednesday.

The result for me of new Date("2014-08-07T00:00Z") is Wed Aug 06 2014 18:00:00 GMT-0600 (MDT). This is because dates are converted to your local timezone when constructed

Upvotes: 4

Related Questions