Moment class

A subclass of DateTime. Therefore:

Moment.now() is DateTime == true // always

Set a global localization:

Global localizations will affect all moment instances, except those with explicitly set localizations.

Moment.setGlobalLocalization(MomentLocalizations.fr());

An instance with explicit localization:

Moment.now(localization: MomentLocalizations.mn())

Formatting

Moment().format will format DateTime based on the payload. For example:

Moment.fromMillisecondsSinceEpoch(0, isUtc: true).LLLL; // Thursday, January 1 1970 00:00 AM

See FormatterTokens

Inheritance
Available extensions

Constructors

Moment(DateTime dateTime, {MomentLocalization? localization})
A Moment created using given dateTime;
Moment.fromMicrosecondsSinceEpoch(int microsecondsSinceEpoch, {bool isUtc = false, MomentLocalization? localization})
Moment.fromMillisecondsSinceEpoch(int millisecondsSinceEpoch, {bool isUtc = false, MomentLocalization? localization})
Moment.now({MomentLocalization? localization})
A Moment created using DateTime.now(), in Local timezone
Moment.nowWithTimezone(bool isUtc, {MomentLocalization? localization})
factory

Properties

day int
The day of the month [1..31].
no setterinherited
hasExplicitLocalization bool
no setter
hashCode int
The hash code for this object.
no setterinherited
hour int
The hour of the day, expressed as in a 24-hour clock [0..23].
no setterinherited
isUtc bool
True if this DateTime is set to UTC time.
finalinherited
LL String
no setter
ll String
no setter
LLL String
no setter
lll String
no setter
LLLL String
no setter
llll String
no setter
localization MomentLocalization
Returns localization of this instance. If this doesn't have localization, returns the global localization.
no setter
LT String
no setter
LTS String
no setter
microsecond int
The microsecond [0...999].
no setterinherited
microsecondsSinceEpoch int
The number of microseconds since the "Unix epoch" 1970-01-01T00:00:00Z (UTC).
no setterinherited
millisecond int
The millisecond [0...999].
no setterinherited
millisecondsSinceEpoch int
The number of milliseconds since the "Unix epoch" 1970-01-01T00:00:00Z (UTC).
no setterinherited
minute int
The minute [0...59].
no setterinherited
month int
The month [1..12].
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
second int
The second [0...59].
no setterinherited
setLocalization MomentLocalization?
Original localization of this instance
no setter
timeZoneName String
The time zone name.
no setterinherited
timeZoneOffset Duration
The time zone offset, which is the difference between local time and UTC.
no setterinherited
weekday int
The day of the week monday..sunday.
no setterinherited
year int
The year.
no setterinherited

Methods

add(Duration duration) Moment
Returns new Moment with added duration
override
calendar({DateTime? reference, bool omitHours = false, String? customFormat}) String
Returns calendar string in accordance with the reference, such as Yesterday, Last Sunday, or default date format concatenated with default hour format. Default formats are derived from the current localization.
compareTo(DateTime other) int
Compares this DateTime object to other, returning zero if the values are equal.
inherited
copyWith({DateTime? dateTime, MomentLocalization? localization}) Moment
Returns new Moment with new values
difference(DateTime other) Duration
Returns a Duration with the difference when subtracting other from this DateTime.
inherited
format([String payload = MomentLocalization.localizationDefaultDateFormat, bool forceLocal = false]) String
Formats date according to payload. See also: FormatterToken
formatDate([bool forceLocal = false]) String
Localization Default formatters
formatDateShort([bool forceLocal = false]) String
formatDateTime([bool forceLocal = false]) String
formatDateTimeShort([bool forceLocal = false]) String
formatDateTimeWithWeekday([bool forceLocal = false]) String
formatDateTimeWithWeekdayShort([bool forceLocal = false]) String
formatTime([bool forceLocal = false]) String
formatTimeWithSeconds([bool forceLocal = false]) String
from(DateTime anchor, {bool dropPrefixOrSuffix = false, Abbreviation form = Abbreviation.none}) String
This will not return precise duration. See MomentLocalization.relativeThreshold for details
fromNow({bool dropPrefixOrSuffix = false, Abbreviation form = Abbreviation.none}) String
Example when using LocalizationEnUs:
fromNowPrecise({bool round = true, bool omitZeros = true, bool includeWeeks = true, Abbreviation form = Abbreviation.none, String? delimiter, DurationFormat format = DurationFormat.auto, bool dropPrefixOrSuffix = false}) String
Example when using LocalizationEnUs:
fromPrecise(DateTime anchor, {bool round = true, bool omitZeros = true, bool includeWeeks = true, Abbreviation form = Abbreviation.none, String? delimiter, DurationFormat format = DurationFormat.auto, bool dropPrefixOrSuffix = false}) String
This will return precise durations. For imprecise durations, use from()
isAfter(DateTime other) bool
Whether this DateTime occurs after other.
inherited
isAtSameMomentAs(DateTime other) bool
Whether this DateTime occurs at the same moment as other.
inherited
isBefore(DateTime other) bool
Whether this DateTime occurs before other.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
subtract(Duration duration) Moment
Returns new Moment with subtracted duration
override
timeZoneFormatted([bool seperateWithColon = true]) String
toIso8601String() String
Returns an ISO-8601 full-precision extended format representation.
inherited
toLocal() Moment
Returns this DateTime value in the local time zone.
override
toString() String
Returns "LLL" formatted string
override
toUtc() Moment
Returns this DateTime value in the UTC time zone.
override

Operators

operator +(Duration duration) Moment
Returns new Moment with added duration
operator -(Duration duration) Moment
Returns new Moment with subtracted duration
operator ==(Object other) bool
Whether other is a DateTime at the same moment and in the same time zone (UTC or local).
inherited

Static Properties

defaultLocalization MomentLocalization
Any instances without explicit localization set will use the global localization.
getter/setter pair
epoch DateTime
epoch, but in the local timezone
getter/setter pair
epochUtc DateTime
epoch in UTC
getter/setter pair
maxValue DateTime
getter/setter pair
minValue DateTime
getter/setter pair

Static Methods

duration(Duration duration, {MomentLocalization? localization, bool round = true, bool omitZeros = true, bool includeWeeks = false, Abbreviation form = Abbreviation.none, String? delimiter, DurationFormat format = DurationFormat.auto, bool dropPrefixOrSuffix = false}) String
Returns precise duration for duration in localization
endOfLastHour() DateTime
End of the last hour in the local timezone
endOfLastIsoWeek() DateTime
End of the last ISO week in the local timezone
endOfLastLocalWeek([int? weekStart]) DateTime
End of the last local week in the local timezone
endOfLastMonth() DateTime
End of the last month in the local timezone
endOfLastYear() DateTime
End of the last year in the local timezone
endOfNextHour() DateTime
End of the next hour in the local timezone
endOfNextIsoWeek() DateTime
End of the next ISO week in the local timezone
endOfNextLocalWeek([int? weekStart]) DateTime
End of the next local week in the local timezone
endOfNextMonth() DateTime
End of the next month in the local timezone
endOfNextYear() DateTime
End of the next year in the local timezone
endOfThisHour() DateTime
End of the current hour in the local timezone
endOfThisIsoWeek() DateTime
End of the current ISO week in the local timezone
endOfThisLocalWeek([int? weekStart]) DateTime
End of the current local week in the local timezone
endOfThisMonth() DateTime
End of the current month in the local timezone
endOfThisYear() DateTime
End of the current year in the local timezone
endOfToday() DateTime
End of today in the local timezone
endOfTomorrow() DateTime
End of tomorrow in the local timezone
endOfYesterday() DateTime
End of yesterday in the local timezone
parse(String input, {MomentLocalization? localization}) Moment
Uses DateTime.parse
override
setGlobalLocalization(MomentLocalization localization) → dynamic
Any instances without explicit localization set will use the global localization.
startOfLastHour() DateTime
Start of the last hour in the local timezone
startOfLastIsoWeek() DateTime
Start of the last ISO week in the local timezone
startOfLastLocalWeek([int? weekStart]) DateTime
Start of the last local week in the local timezone
startOfLastMonth() DateTime
Start of the last month in the local timezone
startOfLastYear() DateTime
Start of the last year in the local timezone
startOfNextHour() DateTime
Start of the next hour in the local timezone
startOfNextIsoWeek() DateTime
Start of the next ISO week in the local timezone
startOfNextLocalWeek([int? weekStart]) DateTime
Start of the next local week in the local timezone
startOfNextMonth() DateTime
Start of the next month in the local timezone
startOfNextYear() DateTime
Start of the next year in the local timezone
startOfThisHour() DateTime
Start of the current hour in the local timezone
startOfThisIsoWeek() DateTime
Start of the current ISO week in the local timezone
startOfThisLocalWeek([int? weekStart]) DateTime
Start of the current local week in the local timezone
startOfThisMonth() DateTime
Start of the current month in the local timezone
startOfThisYear() DateTime
Start of the current year in the local timezone
startOfToday() DateTime
Start of today in the local timezone
startOfTomorrow() DateTime
Start of tomorrow in the local timezone
startOfYesterday() DateTime
Start of yesterday in the local timezone
tryParse(String input, {MomentLocalization? localization}) Moment?
Uses DateTime.tryParse
override