getFullYear returns year before on first day of year

前端 未结 3 988
春和景丽
春和景丽 2021-01-12 11:11

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         


        
3条回答
  •  礼貌的吻别
    2021-01-12 11:16

    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().

提交回复
热议问题