Javascript date 01/01/0001

后端 未结 5 1285
慢半拍i
慢半拍i 2020-12-24 05:46

I want to create a Date object in javascript, which represents the year 0001, 2014 years ago.

I tried with

d = new Date(); d.setYear(1);
console.log         


        
5条回答
  •  被撕碎了的回忆
    2020-12-24 06:14

    The (deprecated) Date.setYear method sets the year for a specified date, but note that:

    If yearValue is a number between 0 and 99 (inclusive), then the year for dateObj is set to 1900 + yearValue. Otherwise, the year for dateObj is set to yearValue.

    var theBigDay = new Date();
    
    theBigDay.setYear(96);   // sets year to 1996
    theBigDay.setYear(1996); // sets year to 1996
    theBigDay.setYear(2000); // sets year to 2000
    

    You should use Date.setFullYear method nevertheless. The other method is deprecated.

提交回复
热议问题