Skip to content

Commit

Permalink
fix: type errors for dates
Browse files Browse the repository at this point in the history
  • Loading branch information
frytg committed Dec 22, 2024
1 parent 743b664 commit 6d56095
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions dates/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- fix: added `@deno-types` to `DateTime` import
- fix: removed `jsr:` prefix from ts examples
- fix: type errors for dates

## 2024-12-12 - 0.1.0

Expand Down
17 changes: 12 additions & 5 deletions dates/dates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const getNow = (): DateTime => DateTime.now()
* getUnix(getNow())
* ```
*/
export const getUnix = (date?: DateTime): number => msToUnix((date || getNow()).valueOf())
export const getUnix = (date?: DateTime): number => msToUnix(getDateTime(date).valueOf())

/**
* Get ms (milliseconds) timestamp
Expand All @@ -88,7 +88,14 @@ export const getUnix = (date?: DateTime): number => msToUnix((date || getNow()).
* getMs(getNow())
* ```
*/
export const getMs = (date?: DateTime): number => (date || getNow()).valueOf()
export const getMs = (date?: DateTime): number => getDateTime(date).valueOf()

/**
* Internal util to get DateTime object
* @param {DateTime} [date] - date object
* @returns {DateTime} DateTime object
*/
const getDateTime = (date?: DateTime): DateTime => (date instanceof DateTime ? date : getNow())

/**
* Get ISO string in UTC timezone
Expand All @@ -103,7 +110,7 @@ export const getMs = (date?: DateTime): number => (date || getNow()).valueOf()
* getISO(getNow())
* ```
*/
export const getISO = (date?: DateTime): string => (date || getNow()).toUTC().toISO()
export const getISO = (date?: DateTime): string | null => getDateTime(date).toUTC().toISO()

/**
* Get ISO string (alias for {@link getISO})
Expand Down Expand Up @@ -140,7 +147,7 @@ export const getMsOffset = (ms: number): number => getMs() - ms
* getRelative(getNow(), 'nl-NL')
* ```
*/
export const getRelative = (date: DateTime, locale = 'en-US'): string => date.toRelative({ locale })
export const getRelative = (date: DateTime, locale = 'en-US'): string | null => date.toRelative({ locale })

/**
* Get year-month-day (YYYYMMDD or YYYY-MM-DD)
Expand All @@ -157,7 +164,7 @@ export const getRelative = (date: DateTime, locale = 'en-US'): string => date.to
* ```
*/
export const getYearMonthDay = (date?: DateTime, withDashes = false): string | number =>
withDashes ? (date || getNow()).toFormat('yyyy-LL-dd') : Number.parseInt((date || getNow()).toFormat('yyyyLLdd'))
withDashes ? getDateTime(date).toFormat('yyyy-LL-dd') : Number.parseInt(getDateTime(date).toFormat('yyyyLLdd'))

/**
* Parse ISO string
Expand Down
1 change: 1 addition & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6d56095

Please sign in to comment.