简体   繁体   English

在Moment.js中格式化longDateFormat

[英]Format longDateFormat in Moment.js

I asked an earlier question about formatting AM/PM via Moment.js meridiem function. 我问了一个关于通过Moment.js meridiem函数格式化AM / PM的问题。 BTW I am using version 2.9 顺便说一句我正在使用2.9版

I was successful in adding periods to the AM/PM input and applying lowercase by using the this 通过使用this,我成功地在AM / PM输入中添加了句点并应用了小写字母

moment.locale('en', {
    meridiem: function(hour, minute, isLower) {
       if (hour < 12) {
          return isLower ? 'a.m.' : 'A.M.';
       } else {
          return isLower ? 'p.m' : 'P.M.';
       }
    }
});

Now I have run into an issue where I need to do the same type of formatting for a longDateFormat string. 现在我遇到了一个问题,我需要对longDateFormat字符串进行相同类型的格式化。

The string is "h:mm:ss a" and the time returned would be the current time but with an uppercase AM/PM 字符串为"h:mm:ss a" ,返回的时间为当前时间,但AM / PM为大写

I need the current time returned but the a input should be formatted ampm 我需要当前返回的时间,但输入应格式化为ampm

in the moment docs longDateFormat is an object not a function how would I go about formatting the meridiem here? 目前文档longDateFormat是一个对象而不是一个函数,我将如何在此处格式化该子午线?

********************EDIT**************** ********************编辑****************

Here is an example of my issue. 这是我的问题的一个例子。

the meridiem setting is working fine with an haz input. meridiem设置在使用haz输入时可以正常工作。 it is lowercase and has periods. 它是小写字母,并且带有句点。 在此处输入图片说明

the meridiem setting doesn't affect a longDateFormat string. meridiem设置不会影响longDateFormat字符串。 The am/pm is always uppercase with no periods. 上午/下午始终是大写字母,没有句点。 No matter if I change the meridiem settings I applied initially. 无论我是否更改了最初应用的子午线设置。 How can I change this? 我该如何更改?

HTML HTML 在此处输入图片说明

{{last_update|date:"MMMM d, y 'at' h:mm:ss a"}}.

Result 结果 在此处输入图片说明

Last updated on July 6, 2016 at 8:25:00 AM.

Thanks 谢谢

Since you are using angular, you have to use angular-moment to take advantage of moment inside directive and filters. 由于使用的是角度,因此必须使用角度矩才能利用指令和过滤器中的矩量。

angular-moment provides a amDateFormat filter that Format dates using moment.js format() method as the docs say. angular-moment提供了一个amDateFormat过滤器,该过滤器使用 docs所说的moment.js format()方法格式化日期 The following code can help to get what you desire: 以下代码可以帮助您获得所需的东西:

 angular.module('MyApp',['angularMoment']) .run(function(){ moment.locale('en', { meridiem: function(hour, minute, isLower) { if (hour < 12) { return isLower ? 'am' : 'AM'; } else { return isLower ? 'pm' : 'PM'; } } }); }) .controller('AppCtrl', function($scope) { $scope.last_update = new Date(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-moment/0.10.3/angular-moment.min.js"></script> <div ng-app="MyApp" ng-controller="AppCtrl"> With angular default date filter: <br/> {{last_update|date:"MMMM d, y 'at' h:mm:ss a"}} <br/> With angular-moment date filter: <br/> {{last_update|amDateFormat:'dddd, MMMM Do YYYY, h:mm:ss a'}} </div> 

In your example you are using angular default date filter . 在您的示例中,您正在使用角度默认日期过滤器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM