Skip to content

Commit

Permalink
feat: expose function to convert dates
Browse files Browse the repository at this point in the history
  • Loading branch information
tomzemp authored May 15, 2024
2 parents 8c442ba + 9d42640 commit 622f0eb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './hooks'
export * as constants from './constants'
export * from './period-calculation'
export { getNowInCalendar, validateDateString } from './utils'
export { getNowInCalendar, validateDateString, convertDate } from './utils'
15 changes: 15 additions & 0 deletions src/utils/convert-date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Temporal } from '@js-temporal/polyfill'
import { NepaliCalendar } from '../custom-calendars/nepaliCalendar'
import { SupportedCalendar } from '../types'

type ConvertDateFn = (
date: string | Temporal.PlainDate,
calendar: SupportedCalendar
) => Temporal.PlainDate

export const convertDate: ConvertDateFn = (date, calendar) => {
if (calendar === 'nepali') {
return Temporal.PlainDate.from(date).withCalendar(new NepaliCalendar())
}
return Temporal.PlainDate.from(date).withCalendar(calendar)
}
2 changes: 2 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export { default as getNowInCalendar } from './getNowInCalendar'
export * from './helpers'
export { default as localisationHelpers } from './localisationHelpers'
export { validateDateString } from './validate-date-string'

export { convertDate } from './convert-date'

0 comments on commit 622f0eb

Please sign in to comment.