Moment.apex

  • Docs
  • Tutorials
Docs Menu
  • Conversion
  • Methods
  • Duration

Conversion Guide

Conversion of Moments

Moment Constructors

Moment.apex has various constructors to build an instance in any situation.

ConstructorDescription
Moment()Create a Moment based on now
Moment(Datetime)Create a Moment based on the Datetime
Moment(Date)Create a Moment based on the Date, with time fields set to 0
Moment(Time)Create a Moment based on the Time, with the date as today
Moment(Long)Create a Moment based on the time in milliseconds
Moment(String)Create a Moment by parsing the string according to the locale
Moment(String, String)Create a Moment by parsing the string according to the pattern
Moment(String, List)Create a Moment by parsing the string according to a list of patterns
Moment(String, Moment.Format)Create a Moment by parsing the string according to the predefined Moment.Format
Moment(Date, Time)Create a Moment by combining the Date and Time
Moment(Integer, Integer, Integer)Create a Moment with the year, month, and day
Moment(Integer, Integer, Integer, Integer, Integer, Integer)Create a Moment with the year, month, day, hour, minute, and second
Moment(Integer, Integer, Integer, Integer, Integer, Integer, Integer)Create a Moment with the year, month, day, hour, minute, second and millisecond
Moment(Map)Create a Moment with a map of the fields(year, month, day, hour, minute, second, and millisecond)
Moment(List)Create a Moment with a list of the fields(year, month, day, hour, minute, second, and millisecond)
Moment(Moment)Create a Moment with an existing Moment
Moment(Moment.DatetimeInfo)Create a Moment with the DatetimeInfo

Date/Time parsing accepts the same patterns as Date/Time formatting. Below is the table of rules supported.

CharacterDescription
yYear
YWeek year
MMonth in year
dDay in month
aAm/pm marker
HHour in day (0-23)
kHour in day (1-24)
KHour in am/pm (0-11)
hHour in am/pm (1-12)
mMinute in hour
sSecond in minute
SMillisecond
wWeek in year
EDay name in week
uDay number of week (1 = Monday, ..., 7 = Sunday)
WWeek in month
DDay in year
FDay of week in month

Get Date/Time Out of Moment

Moment.apex has various constructors to get Date/Time out of a Moment.

MethodDescription
toDateGet Date from Moment
toTimeGet Time from Moment
toDatetimeGet Datetime from Moment
toDatetimeInfoGet DatetimeInfo from Moment
getTimeGet time in milliseconds from Moment
valueOfGet time in milliseconds from Moment
toListGet a list of field values(year, month, day, hour, minute, second, and millisecond) from Moment
toMapGet a map of field values(year, month, day, hour, minute, second, and millisecond) from Moment
toStringGet a string representation of Moment

Timezone

Moment.apex works only in current timezone, and does not handle GMT Date/Time.

Contribute on Github! Edit this section.