Skip to content

@nepdatejs/core ​

The date engine. It depends on no package. See the guide for explanations and examples.

ts
import { adDate, adToBs, formatDate } from '@nepdatejs/core';

Creating and checking ​

FunctionReturnsNotes
adDate(year, month, day)AdDateAny year. Throws INVALID_DATE
bsDate(year, month, day)BsDateInside the table. Throws INVALID_DATE or OUT_OF_TABLE
createDate(calendar, year, month, day)DateOf<C>Either calendar
isValidDate(calendar, year, month, day)booleanNever throws
daysInMonth(calendar, year, month)number
isAdLeapYear(year)boolean
isAdDate(value), isBsDate(value), isCalendarDate(value)booleanType guards for unknown input
MAX_AD_YEARnumberThe largest AD year supported

Converting ​

FunctionReturnsNotes
adToBs(date)BsDateThrows OUT_OF_TABLE
bsToAd(date)AdDate
toCalendar(date, calendar)DateOf<C>Returns the same date if already in that calendar
toIsoDate(date)stringThe AD day as YYYY-MM-DD
fromIsoDate(text)AdDate | nullReads the date as written
fromJsDate(instant, timeZone?)AdDateThe day of a Date in a zone
fromIsoTimestamp(text, timeZone?){ date, time } | nulltime is HH:MM text
toEpoch(date)EpochDayDays since 1970-01-01
fromEpoch(day, calendar)DateOf<C>
toEpochDay(number)EpochDayCheck a number and brand it as an epoch day
weekdayOf(day)WeekdayThe weekday of an epoch day
minDate(calendar), maxDate(calendar)DateOf<C>The convertible range
isConvertible(date)booleanInside the convertible range
isProjected(date)booleanAfter the last verified BS year

The clock ​

FunctionReturnsNotes
today(calendar?, timeZone?)dateDefault 'AD' in the configured zone
now(timeZone?){ date, time }time has hour and minute

Arithmetic ​

All of these keep the date's calendar and return a new date.

FunctionNotes
addDays(date, days)Negative numbers go back
addMonths(date, months)The day is capped to the month's length
addYears(date, years)The day is capped
startOfMonth(date), endOfMonth(date)
startOfYear(date), endOfYear(date)
startOfWeek(date, weekStartsOn?), endOfWeek(date, weekStartsOn?)
differenceInDays(a, b)A number: days from b to a
eachDayOfInterval(start, end)A generator of every day, both ends included
eachDayOfMonth(date)A list of the month's days

Comparing ​

FunctionReturnsNotes
compareDates(a, b)-1 | 0 | 1For sort
isSameDay(a, b)booleanAccepts null
isSameMonth(a, b), isSameYear(a, b)booleanIn the first date's calendar
isBefore(a, b), isAfter(a, b)boolean
isWithin(date, min?, max?)booleanBoth ends included
clampDate(date, min?, max?)dateMoved to the nearest limit
dayOfWeek(date)Weekday0 is Sunday
isWeekend(date, weekendDays?)boolean

Formatting and parsing ​

FunctionReturnsNotes
formatDate(date, pattern?, locale?)stringSee Formatting
DATE_FORMATSobjectThe named formats
parseDate(text, calendar)date or nullNever throws
parseDateStrict(text, calendar)dateThrows the reason

Fiscal year ​

FunctionReturnsNotes
fiscalYear(date)FiscalYearThe year a date falls in
fiscalYearRange(startYear)FiscalYearFrom its BS start year
formatFiscalYear(fy, style?, locale?)string'label', 'full' or 'cbms'
parseFiscalYear(text)FiscalYear | null
isInFiscalYear(date, fy)boolean
BS_FISCAL_YEAR_START_MONTH4Shrawan

Time ​

FunctionReturnsNotes
timeOfDay(hour, minute?)TimeOfDayThrows INVALID_ARGUMENT
isValidTime(hour, minute)boolean
compareTimes(a, b)-1 | 0 | 1
parseTime(text)TimeOfDay | nullEnglish or Nepali
formatTime(time, { hour12?, locale? })string
toIsoDateTime(date, time, timeZone?)stringWith the zone's offset
fromIsoDateTime(text, timeZone?){ date, time } | null

Languages and digits ​

FunctionReturnsNotes
registerLocale(code, data)Adds or replaces a language
getLocale(code?)LocaleDataEnglish if not registered
hasLocale(code)boolean
monthNames(calendar, locale?, width?)string[]'long' or 'short'
weekdayNames(locale?, width?)string[]'long', 'short' or 'narrow'
formatNumber(value, locale?)stringDigits in the locale
formatMessage(template, values)stringFills in {name} placeholders
toDevanagariDigits(value), toAsciiDigits(text)string

Settings ​

FunctionNotes
configure(changes)See Settings
getConfig()The current settings, frozen
resetConfig()Back to the defaults

The BS table ​

FunctionNotes
getBsData()The table in use
setBsData(table)Replace the table
patchBsYear(year, months)Correct one year, or add one at either end
resetBsData()Back to the bundled table
validateBsData(table)Throws NepDateDataError if it isn't valid
bsFirstYear(), bsLastYear()The table's limits

@nepdatejs/core/data exports the raw table as BS_DATA.

Errors ​

NepDateRangeError and NepDateDataError. See Errors.

Types ​

AdDate, BsDate, CalendarDate, CalendarSystem, DateOf, EpochDay, Weekday, MonthNumber, FiscalYear, BsData, LocaleCode, LocaleData, TimeOfDay, NepDateConfig, DateFormatPattern, NamedFormat, NepDateRangeErrorCode, NepDateDataErrorCode.

Released under the MIT licence.