I\'m trying to pull just the year out of a date, but for some reason on the first day of the year its returning the year previous.
new Date(\'2012-01-01\').g
new Date('2012-01-01')
will parse the date assuming it's in UTC. The created Date
object incorporates your timezone, so it will take that into account when printing out the result of getYear()
. If you're in GMT-anything, that means you're going back to the previous year. You can ignore timezones and deal with just UTC by calling Date.prototype.getUTCFullYear()
.