Durations

MomentJS provides an important feature called durations which handles length of time for given units. In this chapter, you will learn this in detail.

Methods Available with Durations

The following table shows the methods available with duration for different units to be used with moment duration −

MethodSyntax
Creatingmoment.duration(Number, String);moment.duration(Number);moment.duration(Object);moment.duration(String);
Clonemoment.duration().clone();
Humanizemoment.duration().humanize();
Millisecondsmoment.duration().milliseconds();moment.duration().asMilliseconds();
Secondsmoment.duration().seconds();moment.duration().asSeconds();
Minutesmoment.duration().minutes();moment.duration().asMinutes();
Hoursmoment.duration().hours();moment.duration().asHours();
Daysmoment.duration().days();moment.duration().asDays();
Weeksmoment.duration().weeks();moment.duration().asWeeks();
Monthsmoment.duration().months();moment.duration().asMonths();
Yearsmoment.duration().years();moment.duration().asYears();
Add Timemoment.duration().add(Number, String);moment.duration().add(Number);moment.duration().add(Duration);moment.duration().add(Object);
Subtract Timemoment.duration().subtract(Number, String);moment.duration().subtract(Number);moment.duration().subtract(Duration);moment.duration().subtract(Object);
Using Duration with Diffvar duration = moment.duration(x.diff(y))
As Unit of Timemoment.duration().as(String);
Get Unit of Timeduration.get(‘hours’);duration.get(‘minutes’);duration.get(‘seconds’);duration.get(‘milliseconds’);
As JSONmoment.duration().toJSON();
Is a Durationmoment.isDuration(obj);
As ISO 8601 Stringmoment.duration().toISOString();
Localemoment.duration().locale();moment.duration().locale(String);

Comments

Leave a Reply

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