js方法记录----年月日当前月上下个月前一天后一天

获取当前年月日

let now = new Date();

let year = now.getFullYear(); //获取完整的年份(4位,1970-????)

let month = now.getMonth() + 1; //获取当前月份(0-11,0代表1月)

let day = now.getDate()

let year1 = year

let month1 = month>10?month:'0'+month

let day1 = day>10?day:'0'+day

let date = year1+'-'+month1+'-'+day1

this.date = year1+'-'+month1

this.nowDate = {

year: year2,

month: month2,

}

判断是否是当前月

let now = new Date()

let today = now.toISOString().substring(0, 7)

let nowDate = this.nowDate.year + '-' + this.nowDate.month

console.log(this.nowDate);

if (today == nowDate) {

//是当前月

} else {

//不是当前月

}

获取上个月

var arr = this.date.split("-");

var year = arr[0]; //获取当前日期的年份

var month = arr[1]; //获取当前日期的月份

var year2 = year;

var month2 = parseInt(month) - 1;

if (month2 == 0) {

//1月的上一月是前一年的12月

year2 = parseInt(year2) - 1;

month2 = 12;

}

if (month2 < 10) {

//10月之前都需要补0

month2 = "0" + month2;

}

var preMonth = year2 + "-" + month2;

获取下个月

var arr = this.date.split("-");

var year = arr[0]; //获取当前日期的年份

var month = arr[1]; //获取当前日期的月份

var day = arr[2]; //获取当前日期的日

var year2 = year;

var month2 = parseInt(month) + 1;

if (month2 == 13) {

//12月的下月是下年的1月

year2 = parseInt(year2) + 1;

month2 = 1;

}

if (month2 < 10) {

//10月之前都需要补0

month2 = "0" + month2;

}

var nextMonth = year2 + "-" + month2;

获取当前月份总天数以及星期

let now = new Date();

let day = now.getDate()

let year = this.nowDate.year;//获取当前年份

let month = this.nowDate.month;//获取当前月份

let dd = new Date(year, month, 0);//获取当月总天数

let current_month = [];

let weekList = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];

for (let i = 1; i <= dd.getDate(); i++) {

  current_month.push({

    time: i + "日",

    week: weekList[new Date(year+'-'+month+'-'+i).getDay()],//获取对应日期的星期

  });

}

获取前一天

var dd = new Date(date);

dd.setDate(dd.getDate() - 1);

var y = dd.getFullYear();

var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;

var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();

this.date = y + "-" + m + "-" + d

获取后一天

let now = new Date();

let year = now.getFullYear(); //获取完整的年份(4位,1970-????)

let month = now.getMonth() + 1; //获取当前月份(0-11,0代表1月)

let day = now.getDate()

var dd = new Date(date);

let year1 = dd.getFullYear(); //获取完整的年份(4位,1970-????)

let month1 = dd.getMonth() + 1; //获取当前月份(0-11,0代表1月)

let day1 = dd.getDate()

dd.setDate(dd.getDate() + 1);

var y = dd.getFullYear();

var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;

var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();

this.date = y + "-" + m + "-" + d

你可能感兴趣的:(js方法记录----年月日当前月上下个月前一天后一天)