Formatting & Nepali
Custom layout strings in English or Nepali, month and weekday 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.
| Token | Meaning |
|---|---|
YYYY | 4-digit year, e.g. "2083" |
YY | 2-digit year, e.g. "83" |
MMMM | full month name, e.g. "Ashwin" |
MM | 2-digit month, zero-padded |
M | month, no leading zero |
DD | 2-digit day, zero-padded |
D | day, no leading zero |
dddd | full weekday name, e.g. "Thursday" |
ddd | weekday 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"func (d Date) FormatNepali(layout string) (string, error)Like Format, with the same tokens, but rendered in Nepali: numbers in Devanagari digits, MMMM
as the Nepali month name, dddd as the Nepali weekday name and ddd as its short form (the name
without "वार"). Characters that aren't part of a token, including any ASCII digits in the layout,
are copied through unchanged. Added in v0.7.0.
d, _ := bs.NewDate(2083, 6, 6)
n1, _ := d.FormatNepali("YYYY-MM-DD") // "२०८३-०६-०६"
n2, _ := d.FormatNepali("dddd, MMMM D, YYYY") // "मंगलवार, असोज ६, २०८३"
n3, _ := d.FormatNepali("ddd D/M/YY") // "मंगल ६/६/८३"
n4, _ := d.FormatNepali("मिति: YYYY-MM-DD") // "मिति: २०८३-०६-०६"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) // "असोज", nilWeekday names
func WeekdayNameNepali(weekday time.Weekday) (string, error)The Devanagari name of the given weekday, e.g. आइतवार for time.Sunday.
Date.WeekdayNameNepali() is the equivalent method for the weekday a date falls on. Returns an
error wrapping ErrInvalidWeekday for a weekday outside time.Sunday..time.Saturday. For the
English name, use time.Weekday's own String method. Added in v0.7.0.
bs.WeekdayNameNepali(time.Sunday) // "आइतवार", nil
d, _ := bs.NewDate(2083, 6, 6)
d.WeekdayNameNepali() // "मंगलवार", nilThe names follow Hamro Patro's calendar spelling, the same source as the Nepali month names: आइतवार, सोमवार, मंगलवार, बुधवार, बिहिवार, शुक्रवार, शनिवार. The Nepal Academy standard spelling uses -बार instead (आइतबार, …); both are in common use.
Nepali digits
func ToNepaliDigits(s string) stringReplaces every ASCII digit (0-9) in s with its Devanagari equivalent. Other characters pass through unchanged.
func FromNepaliDigits(s string) stringThe inverse of ToNepaliDigits.
bs.ToNepaliDigits("2083-06-06") // "२०८३-०६-०६"
bs.FromNepaliDigits("२०८३-०६-०६") // "2083-06-06"