|
|
@@ -1,13 +1,12 @@
|
|
|
-// 日期工具方法,仅处理到天级别
|
|
|
+import dayjs from "./dayjs";
|
|
|
+
|
|
|
+// 日期工具方法,默认使用印尼时区(GMT+7),仅处理到天级别
|
|
|
|
|
|
/**
|
|
|
- * 将 Date 格式化为 yyyy-MM-dd 字符串
|
|
|
+ * 将 Date 格式化为 yyyy-MM-dd 字符串(默认印尼时区 GMT+7)
|
|
|
*/
|
|
|
export function formatDateToYMD(date: Date): string {
|
|
|
- const year = date.getFullYear();
|
|
|
- const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
|
- const day = String(date.getDate()).padStart(2, "0");
|
|
|
- return `${year}-${month}-${day}`;
|
|
|
+ return dayjs(date).format("YYYY-MM-DD");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -17,8 +16,8 @@ export function getTodayRange(): {
|
|
|
beginDateTime: string;
|
|
|
endDateTime: string;
|
|
|
} {
|
|
|
- const today = new Date();
|
|
|
- const ymd = formatDateToYMD(today);
|
|
|
+ const today = dayjs();
|
|
|
+ const ymd = today.format("YYYY-MM-DD");
|
|
|
return {
|
|
|
beginDateTime: ymd,
|
|
|
endDateTime: ymd,
|
|
|
@@ -32,12 +31,10 @@ export function getLast7DaysRange(): {
|
|
|
beginDateTime: string;
|
|
|
endDateTime: string;
|
|
|
} {
|
|
|
- const today = new Date();
|
|
|
- const endDateTime = formatDateToYMD(today);
|
|
|
-
|
|
|
- const start = new Date(today);
|
|
|
- start.setDate(start.getDate() - 6);
|
|
|
- const beginDateTime = formatDateToYMD(start);
|
|
|
+ const end = dayjs();
|
|
|
+ const start = end.subtract(6, "day");
|
|
|
+ const beginDateTime = start.format("YYYY-MM-DD");
|
|
|
+ const endDateTime = end.format("YYYY-MM-DD");
|
|
|
|
|
|
return {
|
|
|
beginDateTime,
|
|
|
@@ -45,4 +42,15 @@ export function getLast7DaysRange(): {
|
|
|
};
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+/**
|
|
|
+ * 将时间戳/日期格式化为 yyyy-MM-dd HH:mm:ss(默认印尼时区 GMT+7)
|
|
|
+ */
|
|
|
+export function formatTimestamp(
|
|
|
+ timestamp?: number | string | Date,
|
|
|
+ pattern = "YYYY-MM-DD HH:mm:ss",
|
|
|
+): string {
|
|
|
+ if (timestamp === undefined || timestamp === null || timestamp === 0) {
|
|
|
+ return "-";
|
|
|
+ }
|
|
|
+ return dayjs(timestamp).format(pattern);
|
|
|
+}
|