MCPcopy
hub / github.com/imsyy/DailyHotApi / getTime

Function getTime

src/utils/getTime.ts:17–149  ·  view source on GitHub ↗
(timeInput: string | number)

Source from the content-addressed store, hash-verified

15 * @returns 时间戳
16 */
17export const getTime = (timeInput: string | number): number | undefined => {
18 try {
19 let num: number;
20
21 // 处理字符串的情况
22 if (typeof timeInput === "string") {
23 // 尝试将字符串直接转换为数字
24 num = Number(timeInput);
25
26 if (isNaN(num)) {
27 const now = dayjs();
28
29 // 处理 "00:00"
30 if (/^\d{2}:\d{2}$/.test(timeInput)) {
31 const [hour, minute] = timeInput.split(":").map(Number);
32 return now.set("hour", hour).set("minute", minute).set("second", 0).valueOf();
33 }
34
35 // 处理 昨天的时间
36 if (/^昨日\s+\d{2}:\d{2}$/.test(timeInput)) {
37 const timeStr = timeInput.replace("昨日", "").trim();
38 const [hour, minute] = timeStr.split(":").map(Number);
39 return now
40 .subtract(1, "day")
41 .set("hour", hour)
42 .set("minute", minute)
43 .set("second", 0)
44 .valueOf();
45 }
46
47 // 处理 今年的日期
48 if (/^\d{1,2}月\d{1,2}日$/.test(timeInput)) {
49 const [month, day] = timeInput
50 .replace("月", "-")
51 .replace("日", "")
52 .split("-")
53 .map(Number);
54 return now
55 .set("month", month - 1)
56 .set("date", day)
57 .startOf("day")
58 .valueOf();
59 }
60
61 // 处理 今年的日期+时间
62 if (/^\d{1,2}月\d{1,2}日\s+\d{2}:\d{2}$/.test(timeInput)) {
63 const [datePart, timePart] = timeInput.split(" ");
64 const [month, day] = datePart.replace("月", "-").replace("日", "").split("-").map(Number);
65 const [hour, minute] = timePart.split(":").map(Number);
66 return now
67 .set("month", month - 1)
68 .set("date", day)
69 .set("hour", hour)
70 .set("minute", minute)
71 .set("second", 0)
72 .valueOf();
73 }
74

Callers 15

getListFunction · 0.85
getListFunction · 0.85
getListFunction · 0.85
getListFunction · 0.85
getListFunction · 0.85
getListFunction · 0.85
getListFunction · 0.85
getListFunction · 0.85
getListFunction · 0.85
getListFunction · 0.85
getListFunction · 0.85
getListFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected