Relative time (Moment)
NepaliMoment turns dates and durations into text relative to now, in English or Nepali. It works like moment.js’s fromNow / calendar and Intl.RelativeTimeFormat. It accepts both DateTime and NepaliDateTime.
final moment = NepaliMoment();final now = DateTime(2024, 1, 10, 8);
moment.format(DateTime(2024, 1, 7), reference: now); // 3 days agomoment.format(DateTime(2024, 1, 10, 11), reference: now); // in 3 hoursNepaliMoment(language: .nepali).format(DateTime(2024, 1, 7), reference: now);// ३ दिन पहिलेIn real code, leave out reference and it defaults to DateTime.now(). The examples on this page pass a fixed reference so that their output is reproducible.
Creating a formatter
Section titled “Creating a formatter”NepaliMoment({Language? language, MomentStyle style = .long, bool useWeeks = false})| Option | Effect |
|---|---|
language |
Language.english or Language.nepali. The default is NepaliUtils.currentLanguage when the formatter is created. |
style |
MomentStyle.long (3 hours ago) or MomentStyle.short (3 hr. ago / ३ घ. पहिले) |
useWeeks |
describe 7 days up to a month in weeks (3 weeks ago) instead of days |
NepaliMoment(style: .short).format(DateTime(2024, 1, 10, 5), reference: now); // 3 hr. agoNepaliMoment(style: .short, language: .nepali) .format(DateTime(2024, 1, 10, 5), reference: now); // ३ घ. पहिलेNepaliMoment(useWeeks: true).format(DateTime(2023, 12, 20), reference: now); // 3 weeks agoNepaliMoment().format(DateTime(2023, 12, 20), reference: now); // 21 days agoNepaliMoment objects are immutable. Create one and reuse it.
format: time from now
Section titled “format: time from now”format(date, {reference}) describes the time from reference to date. Past dates get … ago / … पहिले, and future dates get in … / … पछि.
moment.format(DateTime(2024, 1, 10, 7, 59, 30), reference: now); // a few seconds agomoment.format(DateTime(2024, 1, 10, 7, 59), reference: now); // a minute agomoment.format(DateTime(2024, 1, 10, 7), reference: now); // an hour agomoment.format(DateTime(2024, 1, 9, 10), reference: now); // a day agomoment.format(DateTime(2023, 12, 10), reference: now); // a month agomoment.format(DateTime(2023, 1, 10), reference: now); // a year agoNepaliMoment(language: .nepali).format(DateTime(2024, 1, 10, 10), reference: now);// २ घण्टा पछिIt also works with BS dates:
moment.format(NepaliDateTime(2080, 9, 1), reference: NepaliDateTime(2080, 9, 6)); // 5 days agoThresholds
Section titled “Thresholds”The text is rounded the same way as in moment.js:
| Span | English | Nepali | Short style (example) |
|---|---|---|---|
| 0–44 seconds | a few seconds | केही क्षण | a few seconds |
| 45–89 seconds | a minute | एक मिनेट | 1 min. |
| 90 seconds – 44 minutes | 2–44 minutes | २–४४ मिनेट | 2 min. |
| 45–89 minutes | an hour | एक घण्टा | 1 hr. |
| 90 minutes – 21 hours | 2–21 hours | २–२१ घण्टा | 2 hr. |
| 22–35 hours | a day | एक दिन | 1 day |
| 36 hours – 25 days | 2–25 days | २–२५ दिन | 2 days |
| 26–45 days | a month | एक महिना | 1 mo. |
| over 45 days – 319 days | 2–10 months | २–१० महिना | 10 mo. |
| 320–547 days | a year | एक वर्ष | 1 yr. |
| 548 days and more | 2 years and more | २ वर्ष … | 2 yr. |
With useWeeks: true, spans from 7 days up to a month are given in weeks (a week, २ हप्ता).
formatDuration: a span without a date
Section titled “formatDuration: a span without a date”moment.formatDuration(const Duration(minutes: 5)); // 5 minutesmoment.formatDuration(const Duration(hours: 22)); // a daymoment.formatDuration(const Duration(days: 3), withSuffix: true); // in 3 daysmoment.formatDuration(const Duration(days: -3), withSuffix: true); // 3 days agoNepaliMoment(language: .nepali).formatDuration(const Duration(hours: 5)); // ५ घण्टाWith withSuffix, a positive duration is in the future and a negative one is in the past.
formatUnit: an exact amount of a unit
Section titled “formatUnit: an exact amount of a unit”formatUnit(value, unit, {numeric = true}) works like Intl.RelativeTimeFormat.format. It doesn’t round, so you choose the number and the unit:
moment.formatUnit(-3, .day); // 3 days agomoment.formatUnit(2, .hour); // in 2 hoursmoment.formatUnit(1.5, .hour); // in 1.5 hoursNepaliMoment(style: .short).formatUnit(-3, .minute); // 3 min. agoNepaliMoment(language: .nepali).formatUnit(3, .week); // ३ हप्ता पछिThe units are MomentUnit.second, minute, hour, day, week, month and year.
With numeric: false, the values -1, 0 and 1 use words where the language has them:
formatUnit(v, unit, numeric: false) |
-1 | 0 | 1 |
|---|---|---|---|
.day |
yesterday / हिजो | today / आज | tomorrow / भोलि |
.week |
last week | this week | next week |
.month |
last month / गत महिना | this month | next month |
.year |
last year | this year | next year |
.second |
1 second ago | now / अहिले | in 1 second |
A value that isn’t finite (double.nan, double.infinity) throws an ArgumentError.
calendar: Today, Yesterday, Last Sunday…
Section titled “calendar: Today, Yesterday, Last Sunday…”calendar(date, {reference}) describes date by its calendar day relative to the day of reference, like moment.js’s calendar():
| Day | English | Nepali |
|---|---|---|
| same day | Today at 9:04 PM | आज २१:०४ |
| the day before | Yesterday at 9:04 PM | हिजो २१:०४ |
| the day after | Tomorrow at 9:30 AM | भोलि ०९:३० |
| 2–6 days before | Last Sunday at 9:04 PM | गएको आइतबार, २१:०४ |
| 2–6 days after | Saturday at 9:04 PM | आउँदो शनिबार, २१:०४ |
| otherwise | 11/8/2080 | २०८०-११-०८ |
final now = DateTime(2024, 1, 10, 8);
moment.calendar(DateTime(2024, 1, 9, 21, 4), reference: now); // Yesterday at 9:04 PMmoment.calendar(DateTime(2024, 1, 7, 21, 4), reference: now); // Last Sunday at 9:04 PMmoment.calendar(DateTime(2024, 2, 20, 21, 4), reference: now); // 11/8/2080NepaliMoment(language: .nepali).calendar(DateTime(2024, 1, 7, 21, 4), reference: now);// गएको आइतबार, २१:०४Days are counted in the time zone of date. Dates more than six days away are formatted with NepaliDateFormat.yMd, in BS.
Shorthands: fromBS and fromAD
Section titled “Shorthands: fromBS and fromAD”These static methods are the same as NepaliMoment(language: language).format(date, reference: referenceDate):
NepaliMoment.fromBS( NepaliDateTime.parse('2076-02-32T18:25:14'), referenceDate: NepaliDateTime.parse('2076-02-32T18:34:14'),); // 9 minutes ago
NepaliMoment.fromAD( DateTime.parse('2019-06-02T18:22:14'), referenceDate: DateTime.parse('2019-06-15T18:34:14'), language: .nepali,); // १३ दिन पहिलेGotchas
Section titled “Gotchas”formatwithoutreferencereads the clock, so the result depends on when it runs.showTodayfrom 3.x is gone. Usecalendar, orformatUnit(0, .day, numeric: false)fortoday.
