Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the need for the javascript function getUTCFullYear()?

Tags:

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?

like image 412
pkario Avatar asked Aug 18 '09 07:08

pkario


Video Answer


3 Answers

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.

like image 63
Jesper Avatar answered Dec 20 '22 10:12

Jesper


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

like image 31
Aaron Digulla Avatar answered Dec 20 '22 11:12

Aaron Digulla


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.

like image 37
Dave Webb Avatar answered Dec 20 '22 09:12

Dave Webb