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.
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
Returns true if this occurs after other.
inherited
isAtSameMomentAs(DateTime other) bool
Returns true if this occurs at the same moment as other.
inherited
isBefore(DateTime other) bool
Returns true if this 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
Returns true if 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

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
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.
tryParse(String input, {MomentLocalization? localization}) Moment?
Uses DateTime.tryParse
override