Horje
How to Use format() on a Moment.js Duration?

The format() function can be used to convert moment objects into human-readable strings, but it does not directly format moment.duration objects. To format durations effectively, you need to work with moment objects by adding durations to a base date and then formatting the resulting date-time.

Run the below command before running the code in your local system:

npm i moment

These are the following approaches that we are going to discuss:

Formatting Duration as a Date-Time String

In this approach, you use moment.format() to format the end date after adding a moment.duration to the current date. However, moment.format() is actually for formatting moment objects, not durations directly. We first calculate the end date by adding the duration to the current moment and then use format() on this resulting moment object to get the desired date-time string.

Example: The below example performs Formatting Duration as a Date-Time String.

JavaScript
// script.js
const moment = require('moment');
let duration = moment.duration({
    days: 2,
    hours: 3,
    minutes: 45,
    seconds: 30
});
let startDate = moment();
let endDate = startDate.add(duration);
let res = endDate.format('YYYY-MM-DD HH:mm:ss');
console.log(`Duration will end at: ${res}`);

Output:

Duration will end at: 2024-07-27 21:15:26

Format Duration in a Date Range Format

In this approach, moment.format() is used to display the start and end dates of a period defined by a moment.duration. The duration is added to the start date to compute the end date. The format() method then converts both dates into a readable string format, allowing us to show the date range in a human-readable format.

Example: The below example Format Duration in a Date Range Format.

JavaScript
// script.js
const moment = require('moment');
let duration = moment.duration({
    months: 1,
    days: 10,
    hours: 5
});
let startDate = moment();
let endDate = startDate.clone().add(duration);
let res1 = startDate.format('YYYY-MM-DD HH:mm:ss');
let res2 = endDate.format('YYYY-MM-DD HH:mm:ss');
console.log(`Duration from ${res1} to ${res2}`); 

Output:

Duration from 2024-07-25 17:31:01 to 2024-09-04 22:31:01

Display Duration Relative to Now

In this approach, we use moment.format() to display a date that is a specific duration from the current moment. First, you calculate a future date by adding the moment.duration to the current date and time. Then, format() is used to present this future date in a human-readable string format, showing how the future date and time will appear.

Example: The below example Display Duration Relative to Now.

JavaScript
// script.js
const moment = require('moment');
let duration = moment.duration({
    days: 2,
    hours: 3,
    minutes: 45,
    seconds: 30
});
let futureDate = moment().add(duration);
let res = futureDate.format('dddd, MMMM Do YYYY, h:mm:ss a');
console.log(`In the future, it will be: ${res}`);

Output:

In the future, it will be: Saturday, July 27th 2024, 9:23:19 pm



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
How to Remove Time From Date with Moment.js? How to Remove Time From Date with Moment.js?
Best Javascript Machine Learning Libraries in 2024 Best Javascript Machine Learning Libraries in 2024
JavaScript's Latest Feature: Temporal API JavaScript's Latest Feature: Temporal API
JavaScript SyntaxError - Unexpected string JavaScript SyntaxError - Unexpected string
How to Create Custom Functions in math.js? How to Create Custom Functions in math.js?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
19