js实现获取昨天日期(非周六周日)

1,介绍,获取昨天日期,如果昨天为周六或者周日,会获取非周末的日期

var currentdate ;
		function getNowFormatDate(day) {
		    let date = new Date();
			
		    if (day) {
		      date.setTime(date.getTime() + 24 * day * 60 * 60 * 1000)
		    }
		    let seperator1 = '-'
		    let year = date.getFullYear()
		    let month = date.getMonth() + 1
		    let strDate = date.getDate()
		    if (month >= 1 && month <= 9) {
		      month = '0' + month
		    }
		    if (strDate >= 0 && strDate <= 9) {
		      strDate = '0' + strDate
		    }
			
		    currentdate = year + seperator1 + month + seperator1 + strDate;
			
			if(new Date(currentdate).getDay()%7==6 ||new Date(currentdate).getDay()%7==0){
				let newDay = Number(day)-1;
				return getNowFormatDate(newDay);
			}else{
				return currentdate;
			}
		    
		  }
		  console.log(getNowFormatDate(-1), '昨天') // 昨天

版权声明:本文为weixin_43989837原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。