JS 中,有个 Date 对象,其中有很多用法令人意想不到。
一、dateObj = new Date(year, month, date[, hours[, minutes[, seconds[,ms]]]])
其中第二个参数 month 的取值范围是 0-11 之间的整数( 1 月至 12 月)。
其它参数就按我们日常生活的理解,比如 date 为 1,表示 1 日。
二、用 getFullYear 而不是 getYear
关于原因,请参见拒绝 getYear,改用 getFullYear。
如:
var d = new Date();
document.write(d.getFullYear());
三、getMonth 返回 0-11
如第一点所述 getMonth 返回的不是 1-12,而是 0-11。
四、getDay 返回星期,getDate 才返回某月中的一天
在 VBScript Day() 函数返回的是某月中的一天,可是在 JS 中, Day 可不表示这个意思,getDay 返回 0-6,分别表示星期日到星期六,而 getDate 才返回某月中的一天。
五、getHours、getMinutes、getSeconds、getMilliseconds 以 s 结尾
以复数 s 结尾,不是 getHour、getMinute、getSecond、getMilliseconds,而 getFullYear、getMonth、getDate 则没有以 s 结尾。