MomentJS - Formatting Date and Time


MomentJS provides formats to display date in different ways. There are methods available which tells the time from a given moment, from the current moment, difference between two moments etc. It can display date in JSON format, Array, Object, String etc.

Methods to Format Date and Time

The following table shows a list of methods available which helps in the displaying/formatting of the date as required.

Method Syntax
Format

moment().format();

moment().format(String);

Time from now

moment().fromNow();

moment().fromNow(Boolean);

Time from X

moment().from (Moment|String|Number|Date|Array);

Time to now

moment().toNow();

moment().toNow(Boolean);

Time to X

moment().to(Moment|String| Number|Date|Array);

moment().to(Moment|String| Number|Date|Array, Boolean);

Calendar Time

moment().calendar();

moment().calendar(referenceTime);

moment().calendar(referenceTime, formats);

Difference

moment().diff(Moment|String| Number|Date|Array);

moment().diff(Moment|String| Number|Date|Array, String);

moment().diff(Moment|String| Number|Date|Array, String, Boolean);

Unix Timestamp(milliseconds)

moment().valueOf();

+moment();

Unix Timestamp(seconds)

moment().unix();

Days in Month

moment().daysInMonth();

As JavaScript Date

moment().toDate();

As Array

moment().toArray();

As JSON

moment().toJSON();

As ISO 8601 String

moment().toISOString();

moment().toISOString(keepOffset);

As Object

moment().toObject();

As String

moment().toString();

Inspect

moment().inspect();

Advertisements