u
This commit is contained in:
1 parent
bce259f165
commit
dc46d8034d
8 files changed
+430
-259
No files matched your search
@@ -119,3 +119,34 @@ export function getStartOfMonth() {
|
||||
export function getEndOfMonth() {
|
||||
return dayjs().endOf('month').format('YYYY-MM-DD')
|
||||
}
|
||||
|
||||
export function normalizeDateTime(
|
||||
input?: string | Date | dayjs.Dayjs,
|
||||
): string {
|
||||
if (!input)
|
||||
return ''
|
||||
|
||||
const raw = typeof input === 'string' ? input.trim() : input
|
||||
let d = dayjs(raw)
|
||||
|
||||
if (!d.isValid())
|
||||
return ''
|
||||
|
||||
// string 才需要“猜精度”,Date / dayjs 默认是完整的
|
||||
if (typeof raw === 'string') {
|
||||
if (/^\d{4}$/.test(raw)) {
|
||||
d = d.startOf('year')
|
||||
}
|
||||
else if (/^\d{4}-\d{2}$/.test(raw)) {
|
||||
d = d.startOf('month')
|
||||
}
|
||||
else if (/^\d{4}-\d{2}-\d{2}$/.test(raw)) {
|
||||
d = d.startOf('day')
|
||||
}
|
||||
else if (/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/.test(raw)) {
|
||||
d = d.second(0)
|
||||
}
|
||||
}
|
||||
|
||||
return d.format('YYYY-MM-DD HH:mm:ss')
|
||||
}
|
||||
Reference in new issue
Block a user