编程语言
首页 > 编程语言> > 分享一个工具方法:日期格式化 & 日期转化,用法与java类SimpleDateFormat类似

分享一个工具方法:日期格式化 & 日期转化,用法与java类SimpleDateFormat类似

作者:互联网

/**
 * y	年(201X)
 * M	年中的月份(1-12)
 * d	月份中的天数(1-31)
 * H	一天中的小时数(0-23)
 * h	am/pm 中的小时数(1-12)
 * m	小时中的分钟数(0-59)
 * s	分钟中的秒数(0-59)
 * S	毫秒数(0-999)
 */
/**
 * 格式化输出日期
 * eq: dateFormat(new Date(),"yyyy-MM-dd HH:mm:ss")
 */
export const dateFormat = (date,format) => {
	let reg = /[yMdHhmsS]/;
	let formatArr = format.match(/(.)\1*/g);
	let formatObj = {
		y:""+date.getFullYear(),
		M:"0"+(date.getMonth()+1),
		d:"0"+date.getDate(),
		H:"0"+date.getHours(),
		h:"0"+(date.getHours()>12?date.getHours()-12:(date.getHours()==0?12:date.getHours())),
		m:"0"+date.getMinutes(),
		s:"0"+date.getSeconds(),
		S:"00"+date.getMilliseconds()
	};
	let str = "";
	for(let i= 0,len = formatArr.length;i<len;i++){
		if(formatArr[i]){
			let char0 = formatArr[i].charAt(0);
			if(reg.test(char0)){
				let charLen = formatArr[i].length;
				str += formatObj[char0].slice(charLen*-1);
			}else{
				str += formatArr[i];
			}
		}
	}
	return str;
},
/**
 * 字符串转化为Date对象
 * eq: parseDate("yyyy-MM-dd HH:mm:ss","2017-10-24 20:33:11")
 */
export const parseDate = (dateStr,format) => {
	if(dateStr.length!==format.length){
		console.warn("parseDate: the length of the dateString and format is not equal");
	}
	let reg = /[yMdHhmsS]/;
	let formatArr = format.match(/(.)\1*/g);
	let date = new Date();
	let formatObj = {
		y:date.setFullYear,
		M:date.setMonth,
		d:date.setDate,
		H:date.setHours,
		h:date.setHours,
		m:date.setMinutes,
		s:date.setSeconds,
		S:date.setMilliseconds
	};
	for(let i = 0,formatLenCount = 0,len = formatArr.length;i<len;i++){
		let formatUnit = formatArr[i];
		if(formatUnit){
			let char0 = formatUnit.charAt(0);
			if(reg.test(char0)){
				let dateUnit = dateStr.substring(formatLenCount,formatLenCount+formatUnit.length);
				char0==="M"?formatObj[char0].call(date,dateUnit-1):formatObj[char0].call(date,dateUnit);
			}
		}
		formatLenCount += formatUnit.length;
	}
	return date;
},

  

/** * y    年(201X) * M    年中的月份(1-12) * d    月份中的天数(1-31) * H    一天中的小时数(0-23) * h    am/pm 中的小时数(1-12) * m    小时中的分钟数(0-59) * s    分钟中的秒数(0-59) * S    毫秒数(0-999) */ /** * 格式化输出日期 * eq: dateFormat(new Date(),"yyyy-MM-dd HH:mm:ss") */ export const dateFormat = (date,format) => {     let reg = /[yMdHhmsS]/;     let formatArr = format.match(/(.)\1*/g);     let formatObj = {         y:""+date.getFullYear(),         M:"0"+(date.getMonth()+1),         d:"0"+date.getDate(),         H:"0"+date.getHours(),         h:"0"+(date.getHours()>12?date.getHours()-12:(date.getHours()==0?12:date.getHours())),         m:"0"+date.getMinutes(),         s:"0"+date.getSeconds(),         S:"00"+date.getMilliseconds()     };     let str = "";     for(let i= 0,len = formatArr.length;i<len;i++){         if(formatArr[i]){             let char0 = formatArr[i].charAt(0);             if(reg.test(char0)){                 let charLen = formatArr[i].length;                 str += formatObj[char0].slice(charLen*-1);             }else{                 str += formatArr[i];             }         }     }     return str; }, /** * 字符串转化为Date对象 * eq: parseDate("yyyy-MM-dd HH:mm:ss","2017-10-24 20:33:11") */ export const parseDate = (dateStr,format) => {     if(dateStr.length!==format.length){         console.warn("parseDate: the length of the dateString and format is not equal");     }     let reg = /[yMdHhmsS]/;     let formatArr = format.match(/(.)\1*/g);     let date = new Date();     let formatObj = {         y:date.setFullYear,         M:date.setMonth,         d:date.setDate,         H:date.setHours,         h:date.setHours,         m:date.setMinutes,         s:date.setSeconds,         S:date.setMilliseconds     };     for(let i = 0,formatLenCount = 0,len = formatArr.length;i<len;i++){         let formatUnit = formatArr[i];         if(formatUnit){             let char0 = formatUnit.charAt(0);             if(reg.test(char0)){                 let dateUnit = dateStr.substring(formatLenCount,formatLenCount+formatUnit.length);// 可以得到 2017 | 10                 char0==="M"?formatObj[char0].call(date,dateUnit-1):formatObj[char0].call(date,dateUnit);             }         }         formatLenCount += formatUnit.length;     }     return date; },

标签:12,java,format,getHours,length,SimpleDateFormat,日期,let,date
来源: https://www.cnblogs.com/wangacan/p/15514532.html