Skip to content

Formatting Dates

Formatting is a central feature of bn-calendar. This guide explains how to convert BanglaCalendar date instances into custom formatted strings using tokens, escaping, and built-in formatters.

Overview of Formatting Methods

BanglaCalendar offers three main ways to format dates:

  1. format(pattern): Highly customizable format string engine using tokens.
  2. toString(): Out-of-the-box standard Bengali string ("শুক্রবার, ১ বৈশাখ, ১৪৩০ [গ্রীষ্ম]").
  3. toStringEn(): Out-of-the-box standard English string ("Shukrobar (Friday), 1 Boishakh, 1430 [Grisma (Summer)]").
  4. toJSON(): Standard ISO-like format with zero-padded Bangla digits ("১৪৩০-০১-০১").

Custom Formats with format()

Pass a format pattern string to the .format() method:

typescript
import { 
BanglaCalendar
} from 'bn-calendar';
const
date
= new
BanglaCalendar
('2023-04-14');
// Default format: 'ddd, DD mmmm (SS), YYYY বঙ্গাব্দ'
console
.
log
(
date
.
format
());
// Output: "শুক্রবার, ০১ বৈশাখ (গ্রীষ্মকাল), ১৪৩০ বঙ্গাব্দ" // Custom pattern
console
.
log
(
date
.
format
('YYYY-MM-DD'));
// Output: "১৪৩০-০১-০১"
console
.
log
(
date
.
format
('mmmm DD, YYYY'));
// Output: "বৈশাখ ০১, ১৪৩০"
console
.
log
(
date
.
format
('ddd, D mmm YYYY'));
// Output: "শুক্রবার, ১ বৈ ১৪৩০"

Common Format Tokens

CategoryTokenDescriptionExample
YearYYYY / yyyy4-digit Bangla year১৪৩০
YY / yy2-digit Bangla year৩০
MonthMMonth number (1-12)
MMZero-padded month number (01-12)০১
mmmShort month nameবৈ
mmmmFull month nameবৈশাখ
Day/DateDDay of month (1-31)
DDZero-padded day of month (01-31)০১
DoDay of month with cardinal suffix১লা / ১৫ই
WeekdaydShort weekday nameশু
ddWeekday name without 'বার'শুক্র
dddFull weekday nameশুক্রবার
SeasonSSeason nameগ্রীষ্ম
SSSeason name with 'কাল' suffixগ্রীষ্মকাল

Escaping Text

To preserve raw text that shouldn't be interpreted as tokens, wrap the literal characters inside square brackets [...]:

typescript
const date = new BanglaCalendar('2023-04-14');

// [আজ] protects 'd' from being tokenized as short weekday
console.log(date.format('[আজ] ddd')); 
// Output: "আজশুক্রবার" -> with space: date.format('[আজ ]ddd') -> "আজ শুক্রবার"

// Custom document labels
console.log(date.format('[তারিখ:] DD mmmm, YYYY [বঙ্গাব্দ]'));
// Output: "তারিখ: ০১ বৈশাখ, ১৪৩০ বঙ্গাব্দ"

Related API Documentation

Released under the Apache 2.0 License.