Skip to content

Commit cc55bef

Browse files
committed
add time util
1 parent e2585e5 commit cc55bef

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/components/DataManagement.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ import { save, confirm } from "@tauri-apps/plugin-dialog";
294294
import { snippetApi, initDatabase, clipboardApi } from "../services/tauri";
295295
import { toast } from "../composables/useToast";
296296
import { useClipboardSettings } from "../composables/useClipboardSettings";
297+
import { getFormattedTimestampForFilename } from "../utils/time";
297298
298299
const { t } = useI18n();
299300
@@ -333,9 +334,7 @@ const exportSnippets = async () => {
333334
const jsonData = JSON.stringify(snippetsForExport, null, 2);
334335
335336
// 使用 Tauri 的文件对话框保存文件
336-
const fileName = `seekcode-snippets-${
337-
new Date().toISOString().split("T")[0]
338-
}.json`;
337+
const fileName = `seekcode-snippets-${getFormattedTimestampForFilename()}.json`;
339338
340339
try {
341340
const filePath = await save({

src/utils/time.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,21 @@ export function formatTime(dateInput: Date | string): string {
4949

5050
return date.toLocaleDateString(locale.value);
5151
}
52+
53+
/**
54+
* 生成用于文件名的格式化时间戳
55+
* 格式:YYYY-MM-DDTHH-MM-SS
56+
* 使用本地时间,适合作为文件名
57+
*/
58+
export function getFormattedTimestampForFilename(
59+
date: Date = new Date()
60+
): string {
61+
const year = date.getFullYear();
62+
const month = String(date.getMonth() + 1).padStart(2, "0");
63+
const day = String(date.getDate()).padStart(2, "0");
64+
const hours = String(date.getHours()).padStart(2, "0");
65+
const minutes = String(date.getMinutes()).padStart(2, "0");
66+
const seconds = String(date.getSeconds()).padStart(2, "0");
67+
68+
return `${year}-${month}-${day}T${hours}-${minutes}-${seconds}`;
69+
}

0 commit comments

Comments
 (0)