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?
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.
Yes, around new year. If your TZ is -12, the value differs between 31. December, 12:00am and midnight.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With