getFullYear returns year before on first day of year

前端 未结 3 1013
春和景丽
春和景丽 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:33

    new Date(dateString) will return what time it was in Greewich England at that time. You could do this if you want the year you seek:

    new Date('2012-01-01T00:00:00').getFullYear();
    

    Maybe you like this:

    function dateFromString(dateString){
      return new Date(dateString+'T00:00:00');
    }
    console.log(dateFromString('2012-01-01').getFullYear());
    

提交回复
热议问题