Format Moment.js am/pm to include periods/dots

前端 未结 4 1079
再見小時候
再見小時候 2021-01-25 13:56

I\'m running into a small formatting issue with moment\'s a input.

a/A will return AM/am PM/pm but is there a way to format this to include periods?

<
相关标签:
4条回答
  • 2021-01-25 13:58

    Based on @user4040648 answer here is how you can implement it in es6

        moment.updateLocale('en', {
          meridiem(hour, minute, isLowerCase) {
              return hour < 12 ? 'a.m.' : 'p.m.';
          }
        });
    
    0 讨论(0)
  • 2021-01-25 14:13

    Following moment docs you can have the following:

    moment.updateLocale('en', {
      meridiem: function (hour, minute, isLowercase) {
        if( hour >= 12 )
          return isLowercase ? 'p.m.' : 'P.M.';
        else
          return isLowercase ? 'a.m.' : 'A.M.';
      }
    });
    console.log(moment().hour(1).format('HH:mm a'));
    console.log(moment().hour(15).format('HH:mm A'));
    <script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>

    As stated in the documentation, from moment version 1.6.0

    Locale#meridiem should be a callback function that returns the correct string based on hour, minute, and upper/lowercase.

    While on version before 1.6.0:

    Locale#meridiem was a map of upper and lowercase versions of am/pm.

    0 讨论(0)
  • 2021-01-25 14:15

    The meridiem map was in versions 1.5.x and below, after 1.6.0 the meridiem is a function that returns the value (see link).

    0 讨论(0)
  • 2021-01-25 14:23

    The method you used to customize the meridiem applies to versions < 1.6.0. You should provide a function in newer versions to update meridiem. Please see the docs for more info:

    moment.updateLocale('en', {
      meridiem: function(hour, minute, isLowerCase) {
        if (hour < 12) {
          return 'a.m.';
        } else {
          return 'p.m.';
        }
      }
    });
    
    0 讨论(0)
提交回复
热议问题