Open In App

Moment.js Parse Date Format Plugin

Improve
Improve
Like Article
Like
Save
Share
Report

Parse Date Format is a moment.js plugin to can be used to extract a date/time string’s format from a given string. This format can then be later used with other Moment methods.

Write the below command in the terminal to install Date Format Plugin:

npm install moment-parseformat

 

The following are some of the functions in this plugin:

  • parseFormat

The below examples will help to understand some of the methods of the Parse Date Format Plugin.

Example 1:

Javascript




import moment from 'moment';
import parseFormat from 'moment-parseformat';
  
const format = 
    parseFormat('Tuesday, 12th July, 2022 - 12:19 PM');
      
let obj = { 
    year: 1999, month: 4, day: 21,
    minutes: 10, second: 7, milliseconds: 55
};
  
let date = moment(obj);
console.log(date.format(format));


Output:

 

Example 2:

Javascript




import moment from 'moment';
import parseFormat from 'moment-parseformat';
  
const format = parseFormat('10/10/2010',
    { preferredOrder: { '/': 'DMY', '.': 'MDY', '-': 'YMD' } }
);
  
let day = "2010-02-28 13:40:55"
let date = moment(day);
  
console.log(date.format(format));


Output:

 

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



Last Updated : 21 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads