Moment.js Customize AM/PM Parsing
Last Updated :
28 Apr, 2025
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/
Similar Reads
Moment.js Customize AM/PM In this article, we will learn how to customize the AM/PM logic in Moment.js. The meridiem callback function can be used for customizing the meridiem as per locale. It returns the AM/PM string based on the logic of the callback function. Syntax: moment.updateLocale('en', { meridiem: Function }); The
1 min read
Moment.js Parsing Date Moment.js is a date library for JavaScript that parses, validates, manipulates, and formats dates. We can use the moment() function passed with an object to parse dates represented as Date object. The moment() function is used to create a moment using a Date object. Syntax: moment( Date ) Parameters
1 min read
Moment.js Customize Relative Time Moment.js is very easy to customize. In general, you should create a locale setting with your customizations. In this article, we will discuss the moment.js customize relative time in detail with examples. The moment.updateLocale() function allows us to help in obtaining the relative time. It helps
3 min read
Moment.js Customize Ordinal Names Moment.js is a JavaScript date library for parsing, validating, manipulating, and formatting dates. The ordinals of date values of the Moment output is customizable according to our needs. We will be using the moment.updateLocale() method for this. Syntax: moment.updateLocale('en', { ordinal: callba
2 min read
Moment.js Customize Calendar Names Moment.js is very easy to customize. In general, you should create a locale setting with your customizations in the node js. In this article, we will discuss the moment.js Customize Calendar Names in detail with examples. The moment.updateLocale() function allows us to customize calendar objects for
3 min read
Moment.js Customize Month Names In this article, we will discuss the moment.js Customize Month Names in detail with examples. Moment.js is very easy to customize. In general, you should create a locale setting with your customizations. The moment.updateLocale() function allows us to add month names to the locale customization. It
2 min read
Moment.js Parsing Defaults Moment.js is a date library for JavaScript that parses, validates, manipulates, and formats dates. We can use the moment() function by providing only a few parameters and the rest will default to the current day, month, or year and 0 for an hour, minute, second, and millisecond. The moment() functio
1 min read
Moment.js Customize Changing Time Source In this article, we will learn how to customize the moment.js time source as per one's requirements. Customizing/Changing the time source of moment.js: To customize or change the time that moment.js gives, use can assign moment.now() to a custom function that returns the number of milliseconds since
1 min read
Moment.js Parsing Array Moment.js is a date library for JavaScript that parses, validates, manipulates, and formats dates. We can pass an array to the moment() function to parse a date represented in the form of an array. The moment() function is used to create a moment using an array. Syntax: moment(Number[]) Parameters:
1 min read
Moment.js Parsing Special Formats Moment.js is a JavaScript date library for parsing, validating, manipulating, and formatting dates. In this article, we will learn about the parsing of Special Formats in Moment.js. The ISO-8601 format is used as a standard for parsing the date and time. We can also specify HTML5 constants that can
2 min read