各种时间日期处理收集

当前日期:

function currentTime() {
	var now = new Date();
	var year = now.getFullYear();		// 年
	var month = now.getMonth() + 1;		// 月
	var day = now.getDate();		// 日

	var hh = now.getHours();	// 时
	var mm = now.getMinutes();		// 分

	var time = year + '-' + (month < 10 ? ('0' + month) : month) + '-' + (day < 10 ? ('0' + day ) : day ) + ' ' + (hh < 10 ? ('0' + hh ) : hh) + ':' + (mm< 10 ? ('0' + mm) : mm);
	return time;
}

当前时间的时间戳,某日期后三天时间戳:

var currentTime = new Date().getTime();

var date = new Date(getDate);
var nextTime = Date.parse(date) + 3*24*3600*1000;

上一天、下一天

// 上一天
function lastDay(getDate) {
	var curDate = new Date(getDate);
	var preDate = new Date(curDate.getTime() - 24 * 60 * 60 * 1000);	// 前一天
	preDate = preDate.getFullYear() + '-' + (preDate.getMonth() < 9 ? ('0' + (preDate.getMonth() + 1)) : (preDate.getMonth() + 1)) + '-' + (preDate.getDate() < 10 ? ('0' + preDate.getDate()) : preDate.getDate());
	return preDate;
}

// 下一天
function nextDay(getDate) {
	var curDate = new Date(getDate);
	var nextDate = new Date(curDate.getTime() + 24 * 60 * 60 * 1000);	// 前一天
	nextDate = nextDate.getFullYear() + '-' + (nextDate.getMonth() < 9 ? ('0' + (nextDate.getMonth() + 1)) : (nextDate.getMonth() + 1)) + '-' + (nextDate.getDate() < 10 ? ('0' + nextDate.getDate()) : nextDate.getDate());
	return nextDate;
}

上一周、下一周:

// 当前日期,向前或向后的天数
function addDate(date, n) {
	date = new Date(date);
	date.setDate(date.getDate() + n);
	var dateStr = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
	return dateStr;
}

// 上一周
function lastWeek(getDate) {
		var sendDay = addDate(getDate, -7);
		return sendDay;
};

// 下一周
function nextWeek(getDate) {
	var sendDay = addDate(getDate, 7);
	return sendDay;
}

上一个月、下一个月:

// 上一个月
function lastMonth(getMonth) {
	var date = new Date(getMonth);
	var currentMonth = date.getMonth() <= 0 ? '12' : date.getMonth() - 1;
	var currentYear = date.getMonth() <= 0 ? date.getFullYear() - 1 : date.getFullYear();
	var lastDate = currentYear + '-' + (currentMonth < 9 ? '0' + (currentMonth + 1) : (currentMonth + 1));
	return lastDate;
}

// 下一个月
function nextMonth(getMonth) {
	var date = new Date(getMonth);
	var currentMonth = date.getMonth() + 1 > 12 ? '1' : date.getMonth() + 1;
	var currentYear = date.getMonth() + 1 > 12 ? date.getFullYear() + 1 : date.getFullYear();
	var nextDate = currentYear + '-' + (currentMonth < 9 ? '0' + (currentMonth + 1) : (currentMonth + 1));
	return nextDate;
}

日期格式化:

function timeFormat(date) {
	date = new Date(date);
	var y = date.getFullYear();
	var m = (date.getMonth() + 1) < 10 ? ('0' + (date.getMonth() + 1) ) : (date.getMonth() + 1);		// 月
	var d = date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate();		// 日
	return y + "-" + m + "-" + d;
}

当前日期的本周第一天、最后一天:

// 第一天
function getFirstDayOfWeek(date) {
	date = new Date(date);
	var weekday = date.getDay() || 7;		// 获取星期几,getDay()返回值是0(周日)到6(周六)之间的一个整数。0 || 7位7,即weekday的值为1-7
	date.setDate(date.getDate() - weekday + 1);		// 往前算(weekday-1)天,年份、月份会自动变化
	return timeFormat(date);
}

// 最后一天
function getLastDayOfWeek(date) {
	date = new Date(date);
	var weekday = date.getDay() || 7;
	date.setDate(date.getDate() - weekday + 7);
	return timeFormat(date);
}

两个日期内的所有日期

function getAllDate(begin, end, excludeWeekend) {				// 开始日期,结束日期,是否排除周末
	var arr = [];
	if(begin !== "" && end !== "") {
		var ab = begin.split('-');
		var ae = end.split('-');
		var db = new Date();
		db.setUTCFullYear(ab[0], ab[1] - 1, ab[2]);
		var de = new Date();
		de.setUTCFullYear(ae[0], ae[1] - 1, ae[2]);
		var unixDb = db.getTime() - 24 * 60 * 60 * 1000;
		var unixDe = de.getTime() - 24 * 60 * 60 * 1000;
		for(var k = unixDb; k <= unixDe; ) {
			k = k + 24 * 60 * 60 * 1000;
			var newdate = new Date(parseInt(k));
			var year = newdate.getFullYear();
			var month = (newdate.getMonth() + 1) < 10 ? "0" + (newdate.getMonth() + 1) : (newdate.getMonth() + 1);
			var day = newdate.getDate() < 10 ? "0" + newdate.getDate() : newdate.getDate();
			// 判断是否包含周末
			if(excludeWeekend) {
				if(newdate.getDay() !== 0 && newdate.getDay() !== 6) {
					arr.push(year + "-" + month + "-" + day);
				}
			} else {
				arr.push(year + "-" + month + "-" + day);
			}
		}
	}
	return arr;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值