JavascriptのDateオブジェクトの月(month)に注意
 2007.12.09

最近気づいたことなのだが,JavascriptのDateオブジェクトの月の指定には注意が必要である. JavascriptリファレンスのDateオブジェクトのページによれば コンストラクタの一つに
new Date(yr_num, mo_num, day_num
        [, hr_num, min_num, sec_num, ms_num])
こんなのがあって,その引数の説明に
 yr_num, mo_num, day_num 
    Integer values representing part of a date. As an integer 
    value, the month is represented by 0 to 11 with 0=January 
    and 11=December.
こんな事が書いてある. 注目すべきは,mo_numで `0′ が1月で `11’が12月の指定なんです. なんじゃー,その変な仕様は. Date.getMonth()メソッドの返り値も
The value returned by getMonth is an integer between
 0 and 11. 0 corresponds to January, 1 to February, and so 
on.
となっていて1月を表すDateオブジェクトは0を返し,12月には11を返すのでした.
カテゴリー:javascript