JS用了setMonth或setFullYear设置日期对后,getDay获取到的值要就总是1,要么就总是错误的,是什么原因?我的JS代码如下: var nowtime = new Date();
nowtime.setDate(1);
alert(nowtime.getDay());
//nowtime.setMonth(2);
//nowtime.setFullYear(2008);
如果把最后的两行注释掉,那么getDay能正确的获得值,如果后面两行加上的话,那么获得的值就是错的,是什么原因,请高手指教一下。
nowtime.setDate(1);
alert(nowtime.getDay());
//nowtime.setMonth(2);
//nowtime.setFullYear(2008);
如果把最后的两行注释掉,那么getDay能正确的获得值,如果后面两行加上的话,那么获得的值就是错的,是什么原因,请高手指教一下。
var nowtime = new Date();
nowtime.setDate(1); // 设为本月 1 日 (2010-01)
alert(nowtime.getDay()); // 5 . 2010-01-01 确实是星期五呀
没有错误,怎么会有错
月份是从0开始算的,估计你没在意这个
但是var nowtime = new Date();
nowtime.setDate(1);
nowtime.setMonth(2);
nowtime.setFullYear(2008);
alert(nowtime.getDay());如果这样的话,结果是6,你觉得不对了是吗?
那么你使用如下代码var nowtime = new Date();
nowtime.setDate(1);
nowtime.setMonth(2);
nowtime.setFullYear(2008);
alert(nowtime.getDay()+'---'+(nowtime.getMonth()+1)+'----'+nowtime.getYear()+'---'+nowtime.getDate());当前是1月,你设置为2月,但是实际结果是nowtime.getMonth()+1,也就是2008年3月1日,是星期六。没有错的。
那么总结一下,你觉得错误的地方其实就是3楼已经解释的问题
nowtime.getMonth(); //月份是从0开始算的
那么你使用nowtime.getMonth()的话就必须是nowtime.getMonth()+1.这下明白了吗?