在JavaScript中,可以使用Date对象来处理和格式化日期和时间。
function formatDate(fmt, date) { 
	if (typeof date !== 'object') {
		date = !date ? new Date() : new Date(date);
	}
	var o = {
		'M+': date.getMonth() + 1, // 月份
		'd+': date.getDate(), // 日
		'h+': date.getHours(), // 小时
		'm+': date.getMinutes(), // 分
		's+': date.getSeconds(), // 秒
		'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
		'S': date.getMilliseconds() // 毫秒
	}
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
	}
	for (var k in o) {
		if (new RegExp('(' + k + ')').test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
		}
	}
	return fmt;
};
// 时间格式化 【'yyyy-MM-dd hh:mm:ss',时间】使用:
formatDate('yyyy-MM-dd hh:mm:ss') //当前时间在这个函数中,formatDate接收一个字符串格式和时间值,并返回按照指定格式化的日期时间字符串。format字符串中可以包含yyyy、MM、dd、hh、mm和ss等占位符,分别代表年、月、日、小时、分钟和秒。函数通过正则表达式替换对应的部分。
本文内容仅供个人学习/研究/参考使用,不构成任何决策建议或专业指导。分享/转载时请标明原文来源,同时请勿将内容用于商业售卖、虚假宣传等非学习用途哦~感谢您的理解与支持!