Date对象之获取和设置月份getMonth&setMonth

  getMonth() setMonth()
定义 返回表示月份的数字 用于设置月份
用法 dateObj.getMonth() dateObj.setMonth()
参数

Month:必需。0(一月)--11(十二月)之间。-1为去年的最后一个月,12为明年的第一个月,13为明年的第二个月。

day:可选。表示月份的某一天的数值,这个数值1-31之间。如果当月有31天,32为下个月的第一天。

返回值 0(一月)-11(十二月)之间的一个整数 调整过的日期的毫秒数

getMonth()

 var today=new Date();
 console.log(today.getMonth());//得到的是比月份小1的数值,比如现在是2016.6那么得到的数值就是5

setMonth()

1.只有参数month

    var today=new Date();
    console.log(today.setMonth(2));//1522223787346
    console.log(today.getMonth());//2
today.setMonth(2)设置的是当前的月份时间距离1970年1月1日零点的毫秒数,毫秒数是随着你打印时间的变化而变化的。

2.参数month和day

    var today=new Date();
    today.setMonth(3,32);
    console.log(today.getMonth());//4
    console.log(today.getDate());//2

猜你喜欢

转载自blog.csdn.net/weixin_40512519/article/details/81261552