tags:

views:

102

answers:

4

Is there any real case where getUTCFullYear() differs from getFullYear() in javascript?

The same goes for:

getUTCMonth() vs getMonth()

getUTCDate() vs getDate()

Am I missing something here?

EDIT:

See getUTCFullYear() documentation.

Is there any real case where getUTCFullYear() differs from getFullYear() in javascript?

+3  A: 

The getUTC...() methods return the date and time in the UTC timezone, while the other functions return the date and time in the local timezone of the computer that the script is running on.

Sometimes it's convenient to have the date and time in the local timezone, and sometimes it's convenient to have the date and time in a timezone that's independent of the computer's local timezone.

There are ways to convert between timezones in JavaScript, but they're cumbersome.

So, I guess it's just for convenience.

Jesper
+3  A: 

Yes, around new year. If your TZ is -12, the value differs between 31. December, 12:00am and midnight.

Aaron Digulla
+1  A: 

As 31 December goes to 1 January, this happens at different times in different parts of the world. Hence, the UTC time will be different from e.g. the local time in New Zealand - so at some point getFullYear() will differ from getUTCFullYear() by 1.

Vinay Sajip
+2  A: 

The functions you list essentially report the time in different timezones, so to have a case where getUTCFullYear() will differ from getFullYear() it will be on evening of the 31st December if you live West of the Greenwich Meridian.

For example, in you local timezone the time could be 9pm but in UTC it might already be 2am on 1st January so:

var d = new Date();
d.getFullYear() == 2009  //True
d.getUTCFullYear() == 2010  //True

It is confusing since the methods operate on a Date object rather than reporting the current UTC time. But the method says, if this is the time here, what year is it in the UTC timezone. So for 364.75 days of the years reported by the two methods will be the same.

Dave Webb