-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change for converting date to local timezone (#271)
- Loading branch information
1 parent
ef47c0c
commit 347ae0e
Showing
5 changed files
with
55 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import dayjs, { Dayjs } from "dayjs"; | ||
import utc from "dayjs/plugin/utc"; | ||
|
||
|
||
type UnitOfTime = "second" | "minute" | "hour" | "day" | "month" | "year"; | ||
|
||
export const DATE_FORMAT = "YYYY-MM-DD"; | ||
export const DATE_TIME_FORMAT = "YYYY-MM-DD HH:mm"; | ||
|
||
dayjs.extend(utc); | ||
/** | ||
* @param date Input date string | ||
* @param format Valid date format | ||
* @returns Formatted date string | ||
*/ | ||
const formatDate = (date: string | number, format?: string) => { | ||
if (!date) return ""; // Handle null or undefined values | ||
|
||
// server date is in UTC, convert to local timezone | ||
return dayjs.utc(String(date)).local().format(format || DATE_FORMAT); | ||
}; | ||
|
||
const diff = (fromDate: string, toDate: string, unitOfTime: UnitOfTime) => { | ||
return dayjs(fromDate).diff(dayjs(toDate), unitOfTime); | ||
}; | ||
|
||
const add = (date: Date, unit: number, unitOfTime: UnitOfTime) => { | ||
return dayjs(date).add(unit, unitOfTime); | ||
}; | ||
|
||
const dateToISO = (date: Date | Dayjs) => { | ||
return dayjs(date).toISOString(); | ||
}; | ||
|
||
export default { | ||
formatDate, | ||
diff, | ||
add, | ||
dateToISO, | ||
}; |