欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JS的一些时间获取和计算公用方法封装

程序员文章站 2024-03-17 16:00:46
...

又是一个记录博客,不想废话的一天。。。。
自己之前写的时间计算和日期格式化的源码整理,直接看代码吧~~

/**
 * 判断年份是否为润年
 * @param {Number} year
 * @return {Boolean}
 */
isLeapYear: function (year) {
	return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
},

/**
 * 获取某一年份的某一月份的天数
 * @param {Number} year
 * @param {Number} month
 * @return {String}
 */
getMonthDays: function (year, month) {
	return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month] || (isLeapYear(year) ? 29 : 28);
},

/**
 * 获取某年的某天是第几周
 * @param {Number} y
 * @param {Number} m
 * @param {Number} d
 * @returns {Number}
 */
getWeekNumber: function (y, m, d, curDate) {
	var now = curDate;
	if (now == '') {
		now = new Date(y, m - 1, d);
	}
	var year = now.getFullYear(),
		month = now.getMonth(),
		days = now.getDate();
	//那一天是那一年中的第多少天
	for (var i = 0; i < month; i++) {
		days += getMonthDays(year, i);
	}
	//那一年第一天是星期几
	var yearFirstDay = new Date(year, 0, 1).getDay() || 7;
	var week = null;
	if (yearFirstDay == 1) {
		week = Math.ceil(days / yearFirstDay);
	} else {
		days -= (7 - yearFirstDay + 1);
		week = Math.ceil(days / 7) + 1;
	}
	return week;
},
/**
 * 日期转换
 * @return
 */
formatDate: {
	format: function (date, pattern) {
		pattern = pattern || DEFAULT_PATTERN;
		return pattern.replace(SIGN_REGEXP, function ($0) {
			switch ($0.charAt(0)) {
				case 'y': return padding(date.getFullYear(), $0.length);
				case 'M': return padding(date.getMonth() + 1, $0.length);
				case 'd': return padding(date.getDate(), $0.length);
				case 'w': return date.getDay() + 1;
				case 'h': return padding(date.getHours(), $0.length);
				case 'm': return padding(date.getMinutes(), $0.length);
				case 's': return padding(date.getSeconds(), $0.length);
			}
		});
	},
	parse: function (dateString, pattern) {
		var matchs1 = pattern.match(SIGN_REGEXP);
		var matchs2 = dateString.match(/(\d)+/g);
		if (matchs1.length == matchs2.length) {
			var _date = new Date(1970, 0, 1);
			for (var i = 0; i < matchs1.length; i++) {
				var _int = parseInt(matchs2[i]);
				var sign = matchs1[i];
				switch (sign.charAt(0)) {
					case 'y': _date.setFullYear(_int); break;
					case 'M': _date.setMonth(_int - 1); break;
					case 'd': _date.setDate(_int); break;
					case 'h': _date.setHours(_int); break;
					case 'm': _date.setMinutes(_int); break;
					case 's': _date.setSeconds(_int); break;
				}
			}
			return _date;
		}
		return null;
	},
	formatTo: function(dateString, pattern) {
		var date = new Date(dateString);
		pattern  = pattern || DEFAULT_PATTERN;
		return pattern.replace(SIGN_REGEXP, function ($0) {
			switch ($0.charAt(0)) {
				case 'y': return padding(date.getFullYear(), $0.length);
				case 'M': return padding(date.getMonth() + 1, $0.length);
				case 'd': return padding(date.getDate(), $0.length);
				case 'w': return date.getDay() + 1;
				case 'h': return padding(date.getHours(), $0.length);
				case 'm': return padding(date.getMinutes(), $0.length);
				case 's': return padding(date.getSeconds(), $0.length);
			}
		});
	}
},
/**
 * 获取当前时间段
 * @return 
 */
getNowDate: function() {
	var date = new Date();
	var year = date.getFullYear();
	var month = aqqConvert(date.getMonth() + 1);
	var day  = aqqConvert(date.getDate());
	var hour = aqqConvert(date.getHours());
	var minute = aqqConvert(date.getMinutes());
	var second = aqqConvert(date.getSeconds());
	var nowTime = year+month+day+hour+minute+second;
	return nowTime;
},

/**
 * 获取当前时间段
 * @return 
 */
getNowDateByYYYMMDD: function() {
	var date = new Date();
	var year = date.getFullYear();
	var month = aqqConvert(date.getMonth() + 1);
	var day  = aqqConvert(date.getDate());
	var nowTime = year+'-'+month+'-'+day;
	return nowTime;
},

/**
 * 获取要计算时间的前或者后N天
 * @param requestDay 需要开始推算的时间
 * @param days 间隔天数
 * @param type 向前或者向后推算(0:向前推算;1:向后推算)
 * @return 
 */
getAgoOrBackwardData: function(requestDay,days,type) {
	let date = Date.parse(new Date(requestDay+' 00:00:00'));
	if(type == 0){
		date = new Date(date - days * 24 * 3600 * 1000)
	} else {
		date = new Date(date + days * 24 * 3600 * 1000)
	}
	var year = date.getFullYear();
	var month = aqqConvert(date.getMonth() + 1);
	var day = aqqConvert(date.getDate());
	var nowTime = year+'-'+month+'-'+day;
	return nowTime;
},

/**
 * 获取某一天所在周的开始日期以及结束日期
 * @param requestDay 需要获取的时间
 * @return 
 */
getWeekData: function(requestDay) {
	let now = '';
	if(requestDay) {
		now = new Date(requestDay); // 日期
	} else {
		now = new Date(); // 日期
	}
	let nowDayOfWeek = now.getDay(); // 本周的第几天
	// let nowDay = now.getDate()+1; // 当前日
	let nowDay = now.getDate(); // 当前日
	let nowMonth = now.getMonth(); // 当前月
	let nowYear = now.getFullYear(); // 当前年
	let weekStart = this.getWeekStartDate(nowYear, nowMonth, nowDay, nowDayOfWeek)
	let weekEnd = this.getWeekEndDate(nowYear, nowMonth, nowDay, nowDayOfWeek)
	let weekFormat = [weekStart,weekEnd]
	return weekFormat;
},

// 获得某一周的开始日期
getWeekStartDate(nowYear, nowMonth, nowDay, nowDayOfWeek) {
	var day = nowDayOfWeek || 7;
	// let weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek)
	let weekStartDate = new Date(nowYear, nowMonth, nowDay + 1 - day);
	return this.formatDateTo(weekStartDate,'-')
},

// 获得某一周的结束日期
getWeekEndDate(nowYear, nowMonth, nowDay, nowDayOfWeek) {
	// let weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek))
	var day = nowDayOfWeek || 7;
	let weekEndDate = new Date(nowYear, nowMonth, nowDay + 7 - day)
	return this.formatDateTo(weekEndDate,'-')
},

// 日期格式化
formatDateTo(date,splitStr) {
	var myyear = date.getFullYear();
	var mymonth = date.getMonth() + 1;
	var myweekday = date.getDate();
	if (mymonth < 10) {
		mymonth = "0" + mymonth;
	}
	if (myweekday < 10) {
		myweekday = "0" + myweekday;
	}
	return myyear+ splitStr+ mymonth + splitStr + myweekday
},

/**
 * 获取往前时间段
 * @param days 间隔天数
 * @param otherdate 需要开始推算的时间
 * @return 
 */
getRangeDate: function(days,otherdate,pattern) {
	if(undefined !== otherdate && otherdate) {
		var date = new Date(otherdate);
	} else {
		var date = new Date();
	}
	var year = date.getFullYear();
	var month = aqqConvert(date.getMonth() + 1);
	var day = aqqConvert(date.getDate());
	var hour = aqqConvert(date.getHours());
	var minute = aqqConvert(date.getMinutes());
	var second = aqqConvert(date.getSeconds());
	if(pattern =='d') {
		var endTime = year + '-' + month + '-' + day;
	} else {
		var endTime = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
	}
	var date = new Date(date.getTime() - days * 24 * 3600 * 1000);
	var year = date.getFullYear();
	var month = aqqConvert(date.getMonth() + 1);
	var day = aqqConvert(date.getDate());
	var hour = aqqConvert(date.getHours());
	var minute = aqqConvert(date.getMinutes());
	var second = aqqConvert(date.getSeconds());
	if(pattern =='d') {
		var startTime = year + '-' + month + '-' + day;
	} else {
		var startTime = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
	}
	var param = [
		startTime,
		endTime
	];		
	return param;
},

/**
 * 获取过后时间段
 * @param days 间隔天数
 * @param otherdate 需要开始推算的时间
 * @return 
 */
getFutureRangeDate: function(days,otherdate) {
	if(undefined !== otherdate && otherdate) {
		var date = new Date(otherdate);
	} else {
		var date = new Date();
	}
	var year = date.getFullYear();
	var month = aqqConvert(date.getMonth() + 1);
	var day = aqqConvert(date.getDate());
	var hour = aqqConvert(date.getHours());
	var minute = aqqConvert(date.getMinutes());
	var second = aqqConvert(date.getSeconds());
	var startTime = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
	var date = new Date(date.getTime() + days * 24 * 3600 * 1000);
	var year = date.getFullYear();
	var month = aqqConvert(date.getMonth() + 1);
	var day = aqqConvert(date.getDate());
	var hour = aqqConvert(date.getHours());
	var minute = aqqConvert(date.getMinutes());
	var second = aqqConvert(date.getSeconds());
	var endTime = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
	var param = [
		 startTime,
		 endTime
	];
	return param;
},

/**
 * 获得上个月在昨天这一天的日期
 * @param days 间隔天数
 * @param otherdate 需要开始推算的时间
 * @return 
 */
getLastMonthYestdy(){
	var date = new Date();
	var daysInMonth = new Array([0],[31],[28],[31],[30],[31],[30],[31],[31],[30],[31],[30],[31]);
	var endYear = date.getFullYear();
	var endMonth = date.getMonth()+1;
	var endDay = date.getDate();
	var endTime = endYear + '-' + aqqConvert(endMonth) + '-' + aqqConvert(endDay);
	var startYear = endYear;
	var startMonth = endMonth;
	var startDay = endDay;
	if(startYear%4 == 0 && startYear%100 != 0){
	   daysInMonth[2] = 29;
	}
	if(startMonth - 1 == 0){
	   startYear -= 1;
	   startMonth = 12;
	} else {
	   startMonth -= 1;
	}
	startDay = daysInMonth[startMonth] >= startDay ? startDay+1 : daysInMonth[startMonth];
	var startTime = startYear+"-"+aqqConvert(startMonth)+"-"+aqqConvert(startDay);
	var param = [
		startTime,
		endTime
   ];
	return param;
 },
 /**
 * 是否在系统配置时间段内
 * @param min 最小时间
 * @param max 最大时间
 * @param sysInfo 系统信息
 * @return Blooen
 */
bettweenSysDate: function (min, max, sysInfo) {
	if(min == undefined || max == undefined || sysInfo == undefined) {
           return false;
	}
	let sysMin  = sysInfo.SP_SYSTEM_TABLE_MIN.pvalue;
	let sysMax  = sysInfo.SP_SYSTEM_TABLE_MAX.pvalue;
	if(min >=sysMin && max <=sysMax){
		return true;
	}
	return false;
},
相关标签: JavaScript