-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c680546
commit 97ef451
Showing
3 changed files
with
20 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,31 @@ | ||
import dayjs from "dayjs"; | ||
import relativeTime from "dayjs/plugin/relativeTime"; | ||
|
||
dayjs.extend(relativeTime); | ||
|
||
/** | ||
* Formats a given date as a string in the format of "yyyy-mm-dd". | ||
* If the input is `undefined`, returns `null`. | ||
* @param dateInput - The date to format, or `undefined`. | ||
* @param isRelative - If `true`, returns the relative time from now. | ||
* @returns The formatted date as "yyyy-mm-dd", or `null` if input is `undefined`. | ||
*/ | ||
export function formatDate(dateInput: Date | undefined): string | null { | ||
export function formatDate( | ||
dateInput: Date | undefined, | ||
isRelative: boolean = false, | ||
): string | null { | ||
if (!dateInput) { | ||
return null; | ||
} | ||
|
||
const date = new Date(dateInput); | ||
if (isRelative) { | ||
return dayjs(date).fromNow(); | ||
} | ||
|
||
const year = date.getFullYear(); | ||
const month = (date.getMonth() + 1).toString().padStart(2, "0"); | ||
const day = date.getDate().toString().padStart(2, "0"); | ||
|
||
return `${year}-${month}-${day}`; | ||
} |
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