使用时刻Angularjs格式化日期

use*_*029 7 javascript angularjs

我正在尝试使用Angularjs中的时刻格式化日期,但它不适合我.这是我的小提琴http://jsfiddle.net/sed6x5e8/,下面是我的代码.

HTML:

<div ng-app="miniapp">
    <div ng-controller="Ctrl">
        <div>
            Actual Date: {{date}}
            <br><br>
            Formatted Date: {{formattedDate}}
        </div>

    </div>    
</div>
Run Code Online (Sandbox Code Playgroud)

JS:

var $scope;
var app = angular.module('miniapp', [])
function Ctrl($scope) {
    $scope.date = '2/13/2015';
    $scope.formattedDate = moment($scope.date).format('YYYY-MM-DD');
}
Run Code Online (Sandbox Code Playgroud)

rib*_*ies 11

AngularJS有一个内置的日期过滤器.你不需要使用时机,浪费资源.

<span>{{message.time | date}}</span>
Run Code Online (Sandbox Code Playgroud)

https://docs.angularjs.org/api/ng/filter/date

  • Angular是否支持本地化? (4认同)

jso*_*phy 8

我在我的一个项目中成功地使用了角度矩.它有一个过滤器,可以让您格式化日期.自述文件中的示例:

<span>{{message.time | amDateFormat:'dddd, MMMM Do YYYY, h:mm:ss a'}}</span>
Run Code Online (Sandbox Code Playgroud)

  • @ribsies看起来好像你假设OP的要求只停留在格式化日期... moment.js还有其他一些他们可能正在使用的功能.我知道角度的日期过滤器,但我刚刚根据要求回答. (3认同)