Skip to content

Formatting ​

Introduction ​

formatDate turns a date into text. It writes the date in the date's own calendar, so convert first if you want the other one.

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

const date = bsDate(2083, 7, 1);

formatDate(date, 'long'); // → '1 Kartik 2083'
formatDate(date, 'full', 'ne'); // → 'आइतबार, १ कार्तिक २०८३'
formatDate(date, 'j M'); // → '1 Kar'

The signature is formatDate(date, pattern = 'iso', locale?).

Named formats ​

NamePatternEnglishNepali
isoY-m-d2083-07-01२०८३-०७-०१
cbmsY.m.d2083.07.01२०८३.०७.०१
shortj M Y1 Kar 2083१ कार्तिक २०८३
longj F Y1 Kartik 2083१ कार्तिक २०८३
fulll, j F YSunday, 1 Kartik 2083आइतबार, १ कार्तिक २०८३
ts
import { bsDate, formatDate } from '@nepdatejs/core';

const date = bsDate(2083, 7, 1);

formatDate(date); // → '2083-07-01'
formatDate(date, 'cbms'); // → '2083.07.01'
formatDate(date, 'short'); // → '1 Kar 2083'

cbms is the dotted form that Nepal's tax systems use.

Custom patterns ​

Build your own pattern from these letters. Any other character is printed as it is.

TokenMeaningExample
YYear2083
yTwo-digit year83
mMonth, two digits07
nMonth7
dDay, two digits01
jDay1
FMonth nameKartik
MShort month nameKar
lWeekdaySunday
DShort weekdaySun
ts
import { bsDate, formatDate } from '@nepdatejs/core';

const date = bsDate(2083, 7, 1);

formatDate(date, 'd/m/Y'); // → '01/07/2083'
formatDate(date, 'D, j M'); // → 'Sun, 1 Kar'
formatDate(date, 'F Y'); // → 'Kartik 2083'

To print one of these letters as a letter, put a backslash before it:

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

formatDate(bsDate(2083, 7, 1), 'Y \\B\\S'); // → '2083 BS'

Languages ​

Pass 'en' or 'ne' as the third argument. Names and digits both follow it:

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

formatDate(bsDate(2083, 7, 1), 'long', 'ne'); // → '१ कार्तिक २०८३'
formatDate(adDate(2026, 10, 18), 'long', 'ne'); // → '१८ अक्टोबर २०२६'

If you leave it out, nepdate uses the defaultLocale from configure. You can add other languages too. See Languages.

Showing both calendars ​

Convert and format each one. This is common on official documents:

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

const ad = adDate(2026, 10, 18);

`${formatDate(adToBs(ad), 'long')} (${formatDate(ad, 'short')})`; // → '1 Kartik 2083 (18 Oct 2026)'

In React, <DateText> does this for you.

Digits ​

To change digits in text you already have:

ts
import { formatNumber, toAsciiDigits, toDevanagariDigits } from '@nepdatejs/core';

toDevanagariDigits('2083-07-01'); // → '२०८३-०७-०१'
toAsciiDigits('२०८३'); // → '2083'
formatNumber(2083, 'ne'); // → '२०८३'
formatNumber(2083, 'en'); // → '2083'

formatNumber follows the locale, so use it when the language can change.

Month and weekday names ​

ts
import { monthNames, weekdayNames } from '@nepdatejs/core';

monthNames('BS')[0]; // → 'Baisakh'
monthNames('BS', 'ne')[0]; // → 'बैशाख'
monthNames('AD', 'en', 'short')[0]; // → 'Jan'
weekdayNames('ne')[0]; // → 'आइतबार'
weekdayNames('en', 'short')[6]; // → 'Sat'
weekdayNames('en', 'narrow')[0]; // → 'Su'

Released under the MIT licence.