我有以下代碼創建一個新的對象:問題與新的日期
function Person(firstname, lastname, birth) {
this.name = firstname + " " + lastname;
this.bdate = {
\t year: birth.getFullYear(),
\t day: birth.getDate(),
\t month: "January,Febuary,March,April,May,June,July,August,September,October,November,December".split(",")[birth.getMonth() - 1],
\t monthnumber: birth.getMonth(),
\t }
this.age = new Date().getFullYear() - birth.getFullYear();
}
var foo = new Person("Jon", "Doe", new Date(1996, 12, 5));
console.log(foo);
當我做下面的代碼:
var foo = new Person("Jon", "Doe", new Date(1996, 12, 5));
這也正是它應該這樣做:創建一個新的Person對象。
但
這是在控制檯執行FOO當我得到什麼:
foo
Object { name: "Jon Doe", bdate: Object, age: 20 }
當我bdate對象 點擊我得到:
month: undefined
如何解決? PLUS一年是1997年,而不是1996年
編輯:不,這不是重複getutcmonth的是,這個問題是治療12爲0,礦山返回未定義月
ANOTHER編輯:呵呵,我想這acually是重複的: P仍然,你能幫助我嗎?
'12'不是有效的月份。如果你想要十二月,它應該是'新日期(1996,11,5)'。 – Xufox
[Javascript getUTCMonth()返回0可能重複12月?](https://stackoverflow.com/questions/8335276/javascript-getutcmonth-returns-0-for-december) – Xufox