js Date使用

一 Date中获取月份的范围是0-11,不是1-12

所以在使用时需要注意,如果需要通过Date拿到正确的月份,可以拿到month后进行+1操作。

var date=new Date();
console.log(date);
console.log("当前月份:",date.getMonth());
console.log("当前月份:",date.getMonth()+1);

打印结果:

Fri Jul 03 2020 23:54:29 GMT+0800 (中国标准时间)
VM62:3 当前月份: 6
VM62:4 当前月份: 7

 二 Date获取月份中当周,当月的天

var date=new Date();
console.log('当周的第几天',date.getDay());
console.log('当月的第几天',date.getDate());

打印结果:

当周的第几天 4
当月的第几天 20

 

posted @ 2020-07-03 23:56  一首简单的歌  阅读(188)  评论(0编辑  收藏  举报