fix: parseRelativeDate in correct time zone

This commit is contained in:
Ou 2024-10-21 12:16:15 +08:00
parent 4f34b7fdfc
commit 0bc5e9cb85
2 changed files with 5 additions and 7 deletions

View File

@ -21,7 +21,7 @@ export default defineSource(async () => {
title,
id: url,
extra: {
date: parseRelativeDate(relatieveTime).toDateString(),
date: parseRelativeDate(relatieveTime),
info,
},
})

View File

@ -21,8 +21,6 @@ export function tranformToUTC(date: string, format?: string, timezone: string =
return dayjs.tz(date, format, timezone).valueOf()
}
export const day = dayjs
const words = [
{
startAt: dayjs(),
@ -142,7 +140,7 @@ function toDurations(matches: string[]) {
export const parseDate = (date: string | number, ...options: any) => dayjs(date, ...options).toDate()
export function parseRelativeDate(date: string) {
export function parseRelativeDate(date: string, timezone: string = "Asia/Shanghai") {
// 预处理日期字符串 date
const theDate = toDate(date)
@ -194,7 +192,7 @@ export function parseRelativeDate(date: string) {
// 取特殊词对应日零时为起点,加上相应的时间长度
return w.startAt
.set("hour", 0)
.set("hour", (dayjs().tz(timezone).utcOffset() - dayjs().utcOffset() / 60))
.set("minute", 0)
.set("second", 0)
.set("millisecond", 0)
@ -212,10 +210,10 @@ export function parseRelativeDate(date: string) {
if (wordMatches) {
// The default parser of dayjs() can parse '8:00 pm' but not '8:00pm'
// so we need to insert a space in between
return dayjs(`${w.startAt.format("YYYY-MM-DD")} ${/a|pm$/.test(wordMatches[1]) ? wordMatches[1].replace(/a|pm/, " $&") : wordMatches[1]}`).toDate()
return dayjs.tz(`${w.startAt.format("YYYY-MM-DD")} ${/a|pm$/.test(wordMatches[1]) ? wordMatches[1].replace(/a|pm/, " $&") : wordMatches[1]}`, timezone).toDate()
}
}
}
return new Date()
return date
}