Javascript date function showing wrong date

后端 未结 3 1253
抹茶落季
抹茶落季 2021-01-29 13:34

The code below shows the day, month and year as:

6
3
116

Which is obviously wrong dates.

var date= new Date();
var day=date.get         


        
3条回答
  •  清歌不尽
    2021-01-29 14:18

    Encoding in javascript for months from is 0-11 (not 1-12).

    For year, you could use getFullYear() instead of getYear()

    "Date.prototype.getFullYear() Returns the year (4 digits for 4-digit years) of the specified date according to local time."

    See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

提交回复
热议问题