MomentJS - 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 −

Method Syntax
Creating

moment.duration(Number, String);

moment.duration(Number);

moment.duration(Object);

moment.duration(String);

Clone

moment.duration().clone();

Humanize

moment.duration().humanize();

Milliseconds

moment.duration().milliseconds();

moment.duration().asMilliseconds();

Seconds

moment.duration().seconds();

moment.duration().asSeconds();

Minutes

moment.duration().minutes();

moment.duration().asMinutes();

Hours

moment.duration().hours();

moment.duration().asHours();

Days

moment.duration().days();

moment.duration().asDays();

Weeks

moment.duration().weeks();

moment.duration().asWeeks();

Months

moment.duration().months();

moment.duration().asMonths();

Years

moment.duration().years();

moment.duration().asYears();

Add Time

moment.duration().add(Number, String);

moment.duration().add(Number);

moment.duration().add(Duration);

moment.duration().add(Object);

Subtract Time

moment.duration().subtract(Number, String);

moment.duration().subtract(Number);

moment.duration().subtract(Duration);

moment.duration().subtract(Object);

Using Duration with Diff

var duration = moment.duration(x.diff(y))

As Unit of Time

moment.duration().as(String);

Get Unit of Time

duration.get('hours');

duration.get('minutes');

duration.get('seconds');

duration.get('milliseconds');

As JSON

moment.duration().toJSON();

Is a Duration

moment.isDuration(obj);

As ISO 8601 String

moment.duration().toISOString();

Locale

moment.duration().locale();

moment.duration().locale(String);

Advertisements