Reputation: 1291
My moment.version = 2.10.6. From what I understand in that version, this is about the smallest amount of code I need to determine AM/PM:
// AM
var m = moment("2018-05-16 11:59:59 GMT-0600");
m.localeData().isPM(m.format('A')) // false
// PM
var m = moment("2018-05-16 12:00:00 GMT-0600");
m.localeData().isPM(m.format('A')) // true
Is that correct? If so, it seems a little overcomplicated to me. I wanted to do something like:
moment("2018-05-16 11:59:59 GMT-0600").local().isPM()
I thought it would be reasonable to expect that local
would be able to access localeData
internally and localeData
would know how to format the string internally for the AM|PM, and therefore the return value of local
could have an isPM
method.
Upvotes: 2
Views: 520
Reputation: 5880
You can write your own function to check for PM:
function isPM(momentObj) {
if (momentObj.format('A') === 'PM')
return true;
return false;
}
isPM(moment('2018-05-16T18:00:00')); //true
isPM(moment('2018-05-16T10:00:00')); //false
You can also extend moment.prototype
and implement isPM
:
moment.prototype.isPM = function() {
if (this.format('A') === 'PM')
return true;
return false;
}
moment('2018-05-16T18:00:00').isPM(); //true
moment('2018-05-16T10:00:00').isPM(); //false
Upvotes: 1