格式化日期时间方法总结

文章目录

一、格式化时间(年月日时分秒)
javascript 复制代码
/**
 * 日期时间格式化 年月日时分秒
 * @param {string} dateTime
 */
export function dateTimeFormatting(dateTime) {
	if (dateTime.toString().length == 10) {
		dateTime = dateTime * 1000
	}
	let date = new Date(dateTime || '')
	return date.getFullYear() + '-' + (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) +
		'-' + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ' +
		(date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':' + (date.getMinutes() < 10 ? '0' + date
			.getMinutes() : date.getMinutes()) + ':' + (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
}
// 使用
// dateTimeFormatting(new Date());
// dateTimeFormatting(1719477463); // 2024-06-27 16:37:43
// dateTimeFormatting(1719477463000); // 2024-06-27 16:37:43
二、格式化时间(年月日)
javascript 复制代码
/**
 * 日期格式化 年月日
 * @param {*} dateTime 
 */
export function dateFormatting(dateTime, symbol) {
	let date = new Date(dateTime || '')
	let symbols = symbol || '-'
	return date.getFullYear() + (symbols == 'character' ? '年' : symbols) + (date.getMonth() + 1 < 10 ? '0' + (date
			.getMonth() + 1) : date.getMonth() + 1) + (symbols == 'character' ? '月' : symbols) +
		(date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + (symbols == 'character' ? '日' : '')
}
// 使用
// dateFormatting(new Date());
// dateFormatting(1719477463); // 2024-06-27
// dateFormatting(1719477463000); // 2024-06-27
三、日期格式化(获取年份)
javascript 复制代码
/**
 * 日期格式化 获取年份
 * @param {*} dateTime 
 */
export function getYear(dateTime) {
	let date = new Date(dateTime || '')
	return date.getFullYear()
}
// 使用
// getYear(new Date());
// getYear(1719477463); // 2024
// getYear(1719477463000); // 2024
四、格式化时间(月日)
javascript 复制代码
/**
 * 日期格式化 月日格式化
 * @param {*} dateTime 
 */ 
export function monthDayFormatting(dateTime, symbol) {
	let date = new Date(dateTime || '')
	let symbols = symbol || '-'
	return (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + (symbols == 'character' ?
		'月' : symbols) + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + (symbols == 'character' ? '日' :
		'')
}
// 使用
// monthDayFormatting(new Date());
// monthDayFormatting(1719477463); // 06-27
// monthDayFormatting(1719477463000); // 06-27
五、格式化时间(时分秒)
javascript 复制代码
/**
 * 时间格式化 时分秒
 * @param {string} dateTime
 * @returns {string}
 */
export function timeFormatting(dateTime) {
	let date = new Date(dateTime || '')
	return (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':' + (date.getMinutes() < 10 ? '0' + date
		.getMinutes() : date.getMinutes()) + ':' + (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
}
// 使用
// timeFormatting(new Date());
// timeFormatting(1719477463); // 16:37:43
// timeFormatting(1719477463000); // 16:37:43
六、秒 转换为 分秒
javascript 复制代码
/**
 * 秒 转换为 分秒
 * @param {Object} s
 */
export function formatTime(s) {
	let t = '';
	s = Math.floor(s);

	if (s > -1) {
		let min = Math.floor(s / 60) % 60;
		let sec = s % 60;

		if (min < 10) {
			t += "0";
		}
		t += min + ":";

		if (sec < 10) {
			t += "0";
		}
		t += sec;
	}
	return t;
}
// 使用
// formatTime(90); // 01:30
相关推荐
yuki_uix4 分钟前
跨域与安全:CORS、HTTPS 与浏览器安全机制
前端·面试
用户3153247795454 分钟前
React19项目中 FormEdit / FormEditModal 组件封装设计说明
前端·react.js
陆枫Larry6 分钟前
Git 合并冲突实战:`git pull` 失败与 `pull.ff=only` 的那些事
前端
江南月7 分钟前
让智能体边想边做:从 0 理解 ReActAgent 的工作方式
前端·人工智能
YiuChauvin8 分钟前
vue2中使用 AntV G6
javascript·vue.js
袋鱼不重10 分钟前
Hermes Agent 安装与实战:从安装到与 OpenClaw 全方位对比
前端·后端·ai编程
汉秋10 分钟前
iOS 自定义 UICollectionView 拼图布局 + 布局切换动画实践
前端
江南月11 分钟前
让智能体学会自我改进:从 0 理解 ReflectionAgent 的迭代优化
前端·人工智能
尽欢i11 分钟前
前端响应式布局新宠:vw 和 clamp (),你了解吗?
前端·css
沸点小助手14 分钟前
「 AI 整活大赛,正式开擂 & 最近一次面试被问麻了吗」沸点获奖名单公示|本周互动话题上新🎊
前端·人工智能·后端