Open In App

Moment.js Transform Plugin

Last Updated : 21 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Moment-Transform is a plugin that uses patterns to change dates. On individual components (hours, months, etc.) of a Moment instance, you may use the fundamental operations set, add, and subtract.

Write the below command in the terminal to install Transform Plugin:

npm install moment-transform

 

The following are some of the functions in this plugin:

  • transform

The below examples will help to understand some of the methods of the Transform Plugin.

Example 1:

Javascript




import moment from 'moment';
import transform from 'moment-transform';
  
let day=Date.now();
let today=moment(day);
  
console.log("Today: ")
console.log(
    today.format("DD/MM/YYYY-H:mm:ss")
);
let tomorrow = moment().transform(
    '+01/MM/YYYY', 'DD/MM/YYYY'
);
console.log('Tomorrow: ')
console.log(
    tomorrow.format("DD/MM/YYYY-H:mm:ss")
)


Output:

 

Example 2:

Javascript




import moment from 'moment';
import transform from 'moment-transform';
  
let arr=[2001, 0, 21];
let date=moment(arr);
  
console.log(
    date.format("dddd, Do MMM YYYY, h:mm:ss A")
);
date=date.transform('DD/-01/YYYY', 'DD/MM/YYYY')
console.log(
    date.format("dddd, Do MMM YYYY, h:mm:ss A")
);


Output:

 

Reference: https://momentjs.com/docs/#/plugins/transform/



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads