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.

MethodSyntax
Formatmoment().format();moment().format(String);
Time from nowmoment().fromNow();moment().fromNow(Boolean);
Time from Xmoment().from (Moment|String|Number|Date|Array);
Time to nowmoment().toNow();moment().toNow(Boolean);
Time to Xmoment().to(Moment|String| Number|Date|Array);moment().to(Moment|String| Number|Date|Array, Boolean);
Calendar Timemoment().calendar();moment().calendar(referenceTime);moment().calendar(referenceTime, formats);
Differencemoment().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 Monthmoment().daysInMonth();
As JavaScript Datemoment().toDate();
As Arraymoment().toArray();
As JSONmoment().toJSON();
As ISO 8601 Stringmoment().toISOString();moment().toISOString(keepOffset);
As Objectmoment().toObject();
As Stringmoment().toString();
Inspectmoment().inspect();

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *