Files
xiaozhi-esp32-server/main/manager-web/src/utils/date.js
T
6464c81e72 Manager web (#227)
* update:增加前端设计图

* 前端代码优化

* update:底部信息纠正

* 增加:flyio

* update:去除org.quartz

* update:登陆功能

---------

Co-authored-by: hrz <1710360675@qq.com>
Co-authored-by: CGD <3030332422@qq.com>
2025-03-07 10:01:20 +08:00

61 lines
1.3 KiB
JavaScript
Executable File

export const toDate = (date) => {
return isDate(date) ? new Date(date) : null
}
export const isDate = (date) => {
if (date === null || date === undefined) return false
if (isNaN(new Date(date).getTime())) return false
return true
}
export const isDateObject = (val) => {
return val instanceof Date
}
export const formatAddDate = (date, format, addDay) => {
date = toDate(date)
if (!date) {
return ''
}
if (!addDay) {
date.setDate(date.getDate() + addDay)
}
return formatDateTool(date, format || 'yyyy-MM-dd HH:mm:ss')
}
export const formatDate = (date, format) => {
date = toDate(date)
if (!date) return ''
return formatDateTool(date, format || 'yyyy-MM-dd HH:mm:ss')
}
function formatDateTool(date, fmt) {
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(
RegExp.$1,
(date.getFullYear() + '').substr(4 - RegExp.$1.length)
)
}
const o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds()
}
for (const k in o) {
if (new RegExp(`(${k})`).test(fmt)) {
const str = o[k] + ''
fmt = fmt.replace(
RegExp.$1,
RegExp.$1.length === 1 ? str : padLeftZero(str)
)
}
}
return fmt
}
function padLeftZero(str) {
return ('00' + str).substr(str.length)
}