Moment.apex

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

Methods Guide

Moment Instance Methods

Getter/Setter

Moment.apex has getters/setters for Date/Time fields like:

Moment m = new Moment();
m.hour(12); // set the hour
m.hours(12); // set the hour
m.hour(); // get the hour
m.hours(); // get the hour

Below is the table of the fields supporting these getters/setters.

FieldDescription
millisecond(s)Get/set the millisecond
second(s)Get/set the second
minute(s)Get/set the minute
hour(s)Get/set the hour
day(s)Get/set the day
month(s)Get/set the day
year(s)Get/set the year
dayOfWeekGet/set the day of week
dayOfYearGet/set the day of year
week(s)Get/set the week of year
quarter(s)Get/set the quarter of year

Besides, we have generic methods to access Date/Time fields.

Moment m = new Moment();
m.field('year'); // get the year
m.field('year', 2018); // set the year
MethodDescription
fieldGet/set the field

Query

Moment.apex has utility methods for querying.

MethodDescription
weeksInYear()Get the number of weeks in year
startOf(String)Get the start moment of the Date/Time field
endOf(String)Get the end moment of the Date/Time field
debug()Print the debug information
diff(Moment)Create a duration between two moments
daysInMonth()Get the days in the current month
isBefore(Moment)Check if it is before the other moment
isSame(Moment)Check if it is the same as the other moment
isAfter(Moment)Chekc if it is after the other moment
isSameOrBefore(Moment)Check if it is the same or before the other moment
isSameOrAfter(Moment)Check if it is the same or after the other moment
isBetween(Moment, Moment)Check if it is between these two moments
isLeapYear()Check if it is the leap year

Operation

Moment.apex also enables operations like adding/subtracting on fields of Moment.

MethodDescription
add(Moment.Duration)Add a duration
add(Long)Add time in milliseconds
add(Integer, String)Add some units of time
add(Map)Add different units of time
subtract(Moment.Duration)Subtract a duration
subtract(Long)Subtract time in milliseconds
subtract(Integer, String)Subtract some units of time
subtract(Map)Subtract different units of time

Display

Moment.apex includes APIs to display Date/Time.

MethodDescription
format(String)Format the Moment using the format
fromMoment(Moment)Show the duration from the moment with prefix
fromMoment(Moment, Boolean)Show the duration from the moment with or without prefix
fromNow(Boolean)Show the duration from now with or without prefix
fromNow()Show the duration from now with prefix
toMoment(Moment)Show the duration to the moment with prefix
toMoment(Moment, Boolean)Show the duration to the moment with or without prefix
toNow()Show the duration to now with prefix
toNow(Boolean)Show the duration to now with or without prefix

Units

Moment.apex supports the below units, and any shortened strings(case-insensitive) that can uniquely pick a field out.

Moment m = new Moment();
m.field('year'); // standard
m.field('years'); // also work
m.field('yea'); // fine
m.field('y'); // works
m.field('Y'); // cool
UnitDescription
yearyear
monthmonth
dayday
hourhour
minuteminute
secondsecond
millisecondmillisecond
weekweek
quarterquarter

Static Methods

Moment.apex has a few static methods/fields.

MethodDescription
YEARyear
MONTHmonth
DAYday
HOURhour
MINUTEminute
SECONDsecond
MILLISECONDmillisecond
WEEKweek
QUARTERquarter
UNITSa list of units
DURATION_YEARthe milliseconds of year
DURATION_MONTHthe milliseconds of month
DURATION_DAYthe milliseconds of day
DURATION_HOURthe milliseconds of hour
DURATION_MINUTEthe milliseconds of minute
DURATION_SECONDthe milliseconds of second
DURATION_MILLISECONDthe milliseconds of millisecond
DURATION_WEEKthe milliseconds of week
DURATION_QUARTERthe milliseconds of quarter
FORMAT_DATE'dd/MM/yyyy'
FORMAT_TIME'HH:mm:ss'
FORMAT_DATETIME'dd/MM/yyyy HH:mm:ss'
duration(Long)Create a Duration from time in milliseconds
duration(Integer, String)Create a Duration from some units of time
duration(Map)Create a Duration from different units of time
normalize(String)Normalize a string of unit

Contribute on Github! Edit this section.