-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdate.ts
62 lines (57 loc) · 1.67 KB
/
date.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
dayjs.extend(utc);
/**
* 日期时间格式模板
*/
export const DATE_FORMATTER = {
YEAR_FORMAT: "YYYY",
MONTH_FORMAT: "YYYY-MM",
DATE_FORMAT: "YYYY-MM-DD",
HOUR_FORMAT: "YYYY-MM-DD HH",
MINUTE_FORMAT: "YYYY-MM-DD HH:mm",
SECONDS_FORMAT: "YYYY-MM-DD HH:mm:ss",
};
const _customDateFormatter = (v: string | Date, format = DATE_FORMATTER.SECONDS_FORMAT): string => {
if (v) {
const time = dayjs(v).format(format);
// FIXME: 修复时区问题
// if ((typeof v === "string" && v.includes("Z")) || v.constructor === Date) {
// // UTC字符串时间或Date对象格式化为北京时间
// time = dayjs(v).utcOffset(8).format(format);
// } else {
// time = dayjs(v).format(format);
// }
if (time !== "Invalid Date") {
return time;
}
}
return "";
};
/**
* 日期时间格式化为字符串
* @function formatDatetime
*
* @param {string | Date} v 日期时间
* @param {string} format 日期时间格式
* @returns {string} 日期时间字符串
*
* @example
* const date = new Date();
* const strDate = formatDatetime(date, 'YYYY-MM-DD HH:mm:ss');
* console.log('日期时间', strDate);
*/
export const formatDatetime = (v: string | Date, format?: string): string => _customDateFormatter(v, format);
/**
* 日期时间格式化为13位timestamp
* @function formatTimestamp
*
* @param {string | Date} v 日期时间
* @returns {number} 13位timestamp
*
* @example
* const date = new Date();
* const numDate = formatTimestamp(date);
* console.log('日期时间', numDate);
*/
export const formatTimestamp = (v: string | Date): number => dayjs(v).valueOf();