Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose function to convert dates #35

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
Loading