//獲取上個月最后一天 date = new Date(); console.log(date); date.setDate(0); //setDate(day) 方法用於設置一個月的某一天。day取1-31表示對應的天數,另外0 為上一個月的最后一天,-1 為上一個月最后一天之前的一天,32 為下個月的第一天或者第二天 console.log(date); var year = date.getFullYear(); var month = date.getMonth()+1; month = month > 9 ? month:'0'+month; var day = date.getDate(); //getDate() 方法可返回月份的某一天。也就是當前date的當前天數 day = day > 9 ? day:'0'+day; console.log("year:"+year+"month:"+month+"day:"+day) console.log("---------------------我是分割線------------------------") //獲取當月第一天 date = new Date(); var year = date.getFullYear(); var month = date.getMonth()+1; month = month > 9 ? month:'0'+month; console.log("year:"+year+"month:"+month+"day:01")
轉載於:https://www.cnblogs.com/wangylblog/p/13970540.html