js 时间 操作

获取本月有多少天

function getThisMonthCount() {
       var curDate = new Date();
       var curMonth = curDate.getMonth();
       curDate.setMonth(curMonth+1);
       curDate.setDate(0);
       return curDate.getDate();
}

获取上个月有多少天

function getLastMonthCount() {
       var curDate = new Date();
       var curMonth = curDate.getMonth();
       curDate.setMonth(curMonth);
       curDate.setDate(0);
       return curDate.getDate();
}

格式化时间

var formatDate = function (date) { 
		var y = date.getFullYear(); 
		var m = date.getMonth() + 1; 
		m = m < 10 ? ('0' + m) : m; 
		var d = date.getDate(); 
		d = d < 10 ? ('0' + d) : d; 
		var h = date.getHours(); 
		var minute = date.getMinutes(); 
		minute = minute < 10 ? ('0' + minute) : minute;
		var second= date.getSeconds(); 
		second = minute < 10 ? ('0' + second) : second; 
		return y  + m  + d; 
};

formatDate(new Date())

获取本月所有天数

function getMonthDayAll(){
		var nowdays = new Date();  
		var year = nowdays.getFullYear();  
		var month = nowdays.getMonth();  
		if(month==0)  
		{  
		    month=12;  
		    year=year-1;  
		}  
		if (month < 10) {  
		    month = "0" + month;  
		}  
		
		var allDay = [];
		
		
		var MonthDay = getMonthCount();
		for(var index = 1;index <= MonthDay ; index++){
			var day = new Date(year, month, index);
			allDay.push(formatDate(day));
		}
		return allDay;
}

function getMonthCount() {
        var curDate = new Date();
        var curMonth = curDate.getMonth();
       curDate.setMonth(curMonth+1);
       curDate.setDate(0);
       return curDate.getDate();
}

获取上个月的所有日期

function getLastMonthDayAll(){
		var nowdays = new Date();  
		var year = nowdays.getFullYear();  
		var month = nowdays.getMonth();  
		if(month==0)  
		{  
		    month=12;  
		    year=year-1;  
		}  
		if (month < 10) {  
		    month = "0" + month;  
		}  
		
		var allDay = [];
		
		
		var latMonthDay = getLastMonthCount();
		for(var index = 1;index <= latMonthDay; index++){
			var day = new Date(year, month-1, index);
			allDay.push(formatDate(day));
		}
		return allDay;
}

function getLastMonthCount() {
       var curDate = new Date();
       var curMonth = curDate.getMonth();
       curDate.setMonth(curMonth);
       curDate.setDate(0);
       return curDate.getDate();
}

获取上个月第一天

function getLastMonthDayFirst(){
		var nowdays = new Date();  
		var year = nowdays.getFullYear();  
		var month = nowdays.getMonth();  
		if(month==0)  
		{  
		    month=12;  
		    year=year-1;  
		}  
		if (month < 10) {  
		    month = "0" + month;  
		}  
		var firstDay = year  + month + "01";//上个月的第一天  
		return firstDay;
}

 获取上个月最后一天

function getLastMonthDayEnd(){
		var nowdays = new Date();  
		var year = nowdays.getFullYear();  
		var month = nowdays.getMonth();  
		if(month==0) {  
		    month=12;  
		    year=year-1;  
		}  
		if (month < 10) {  
		    month = "0" + month;  
		}
		
		var myDate = new Date(year, month, 0);  //上个月最后一天
		var lastDay = year +  month + myDate.getDate();//上个月的最后一天
		return lastDay;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值