Open In App

Moment.js Customize AM/PM Parsing

Improve
Improve
Like Article
Like
Save
Share
Report

AM/PM Parsing is the process of analyzing the string to check for AM or PM. This means we’re trying to find out whether the given time in string format is in AM or PM. There are two ways to accomplish this. One is by configuring the updateLocale() method and the other is by parsing AM/PM directly from the string. Let’s understand each of them. 

Configuring updateLocale() method

Syntax: 

moment.updateLocale('en', {
    meridiemParse : RegExp
    isPM : Function
});

As we can see that there are 2 ways to parse AM/PM from string by configuring in updateLocale() Method.

  • meridiemParse: Takes a regular expression to check if it satisfies for given string 
  • isPM: a callback function to check if PM is in the string.

isPM Function: The isPM function is used to check whether the given time is a PM time or not. It returns a boolean value. For example – isPM() should return true if the input string is past 12 noon. we are using isPM to check if the current time is in ‘pm’ or not.

moment.updateLocale('en', {
    isPM : function (input) {
        return ((input + '').toLowerCase()[0] === 'p');
    }
});

meridiemParse: In this example, we are using the meridiemParse property which takes a regular expression to check a string for AM/PM

// Specifying what string should be parsed
// as input using meridiemParse property
moment.updateLocale('en', {
     meridiemParse : /[ap]\.?m?\.?/i 
});

Example 1: In this example, we are setting AM/PM parsing logic for the ‘fr’ i.e French Locale and then we are checking the isPM function with a sample string.

Javascript




var localeData = moment.updateLocale('fr', {
    meridiemParse: /PD|MD/,
    isPM: function (input) {
        return input.charAt(0) === 'M';
    }
});
var m = localeData.isPM("MD");


Output:

Meredian : true

Parsing AM/PM directly from a string: We can extract dates by using either ‘A’ or ‘a’ in the format string of moment() to interpret it as a moment and then in format() to show it in output. 

Example 2: Let’s say that we have the following string.

Mon 03-Jul-2017, 11:00 AM

Now, to extract AM/PM from this we can use the following code. We just have to add the letter ‘A’  (for AM/PM) or the letter ‘a’ (for am/pm) notations. Here we have appended ‘A’ in the second parameter of the moment() and in the format().

Javascript




const moment = require('moment');
console.log(
    moment('Mon 03-Jul-2017, 11:00 AM',
    'ddd DD-MMM-YYYY, hh:mm A')
        .format('hh:mm A')
);


Output:

11:00 AM

Example 3: Let’s say that we have the following string

Tue 04-Jul-2017, 11:08 PM

Now, to extract AM/PM from this we can use the following code. We just have to add the letter ‘a’ in the second parameter of the moment() and for the format() in the end to get am/pm notations.

Javascript




console.log(
    moment('Mon 03-Jul-2017, 11:00 AM',
    'ddd DD-MMM-YYYY, hh:mm a')
        .format('hh:mm a')
);


Output:

11:08 pm

Reference: https://momentjs.com/docs/#/customization/am-pm-parsing/



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