Nepali DateNepali DateBikram Sambat Date Engine
Methods

date.format()

Format a NepaliDate instance into a custom string using token patterns for English and Nepali output.

The format method transforms a NepaliDate instance into a formatted string according to a pattern of token specifiers. It supports tokens for year, month, day, weekday, hours, minutes, and seconds, with full support for localized English names as well as Devanagari script numerals and words.


Import

import { NepaliDate } from "nepali-date-library";

Signature

format(formatStr: string): string;

Parameters

ParameterTypeRequiredDescription
formatStrstringYesFormatting template string containing pattern tokens.

Return Value

  • Type: string
  • The formatted date string with all tokens substituted.

Token Reference

Uppercase characters produce English representations, while lowercase characters produce Devanagari (Nepali) representations.

Year Tokens

TokenOutputDescriptionExample (2081 BS)
YYYYEnglish 4-digit yearFull Gregorian-style digit year"2081"
YYYEnglish 3-digit yearLast 3 digits"081"
YYEnglish 2-digit yearLast 2 digits"81"
yyyyNepali 4-digit yearDevanagari numerals"२०८१"
yyyNepali 3-digit yearLast 3 digits in Devanagari"०८१"
yyNepali 2-digit yearLast 2 digits in Devanagari"८१"

Month Tokens

TokenOutputDescriptionExample (Baisakh)
MEnglish 1-digitMonth number (1-12)"1"
MMEnglish 2-digitZero-padded month number (01-12)"01"
MMMEnglish 3-letterAbbreviated month name"Bai"
MMMMEnglish FullComplete romanized month name"Baisakh"
mNepali 1-digitDevanagari month number (१-१२)"१"
mmNepali 2-digitZero-padded Devanagari month number"०१"
mmmNepali ShortAbbreviated Devanagari month name"बै"
mmmmNepali FullComplete Devanagari month name"बैशाख"

Day and Weekday Tokens

TokenOutputDescriptionExample (Day 5, Sunday)
DEnglish 1-digitDay of the month (1-32)"5"
DDEnglish 2-digitZero-padded day of the month (01-32)"05"
DDDEnglish ShortAbbreviated weekday name"Sun"
DDDDEnglish FullComplete weekday name"Sunday"
dNepali 1-digitDevanagari day of the month (१-३२)"५"
ddNepali 2-digitZero-padded Devanagari day of the month"०५"
dddNepali ShortAbbreviated Devanagari weekday"आइत"
ddddNepali FullComplete Devanagari weekday"आइतबार"

Escaping Literal Characters

To include literal characters that conflict with format tokens (such as M, D, Y, m, d, y), wrap the text inside double quotes ("):

import { NepaliDate } from "nepali-date-library";

const date = new NepaliDate("2081-01-25");

const formatted = date.format('"Date:" YYYY-MM-DD');
console.log(formatted); // 'Date: 2081-01-25'

Examples

Standard ISO-Style Formatting

import { NepaliDate } from "nepali-date-library";

const date = new NepaliDate("2081-01-05");

console.log(date.format("YYYY-MM-DD")); // "2081-01-05"
console.log(date.format("YYYY/MM/DD")); // "2081/01/05"

Full English Date with Weekday

import { NepaliDate } from "nepali-date-library";

const date = new NepaliDate("2081-01-25");

console.log(date.format("DDDD, MMMM DD, YYYY"));
// "Thursday, Baisakh 25, 2081"

Native Nepali Devanagari Formatting

import { NepaliDate } from "nepali-date-library";

const date = new NepaliDate("2081-01-25");

console.log(date.format("dddd, mmmm dd, yyyy"));
// "बिहिबार, बैशाख २५, २०८१"

console.log(date.format("yyyy-mm-dd"));
// "२०८१-०१-२५"

Advertisement

On this page