Formatting & Nepali digits

Custom layout strings, month names, and converting between ASCII and Devanagari digits.

func (d Date) Format(layout string) (string, error)

Renders d according to layout, replacing recognized tokens. Any other character (including punctuation and spaces) is copied through unchanged. Returns the usual validation errors if d itself is invalid.

TokenMeaning
YYYY4-digit year, e.g. "2083"
YY2-digit year, e.g. "83"
MMMMfull month name, e.g. "Ashwin"
MM2-digit month, zero-padded
Mmonth, no leading zero
DD2-digit day, zero-padded
Dday, no leading zero
ddddfull weekday name, e.g. "Thursday"
dddweekday abbreviation, e.g. "Thu"
d, _ := bs.NewDate(2083, 6, 6)
 
s1, _ := d.Format("YYYY-MM-DD")           // "2083-06-06"
s2, _ := d.Format("dddd, MMMM D, YYYY")   // "Tuesday, Ashwin 6, 2083"
s3, _ := d.Format("D/M/YY")               // "6/6/83"

There's no MMM (3-letter month abbreviation) token — Nepali month names don't abbreviate unambiguously (Ashadh and Ashwin both start "Ash"). An unsupported MMM in a layout currently decomposes into MM + M rather than erroring.

Month names

func MonthName(month int) (string, error)

The English name of the given 1-based month (1 is Baisakh, 12 is Chaitra). Date.MonthName() is the equivalent method.

func MonthNameNepali(month int) (string, error)

The Devanagari name of the given month, e.g. असोज for Ashwin. Date.MonthNameNepali() is the equivalent method. These were verified against Hamro Patro's own calendar page titles — see calendar data sources.

bs.MonthName(6)       // "Ashwin", nil
bs.MonthNameNepali(6) // "असोज", nil

Nepali digits

func ToNepaliDigits(s string) string

Replaces every ASCII digit (0-9) in s with its Devanagari equivalent. Other characters pass through unchanged.

func FromNepaliDigits(s string) string

The inverse of ToNepaliDigits.

bs.ToNepaliDigits("2083-06-06")     // "२०८३-०६-०६"
bs.FromNepaliDigits("२०८३-०६-०६")   // "2083-06-06"