Format Date time in AngularJS

后端 未结 13 1744
Happy的楠姐
Happy的楠姐 2020-11-30 22:00

How do I properly display the date and time in AngularJS?

The output below shows both the input and the output, with and without the AngularJS date filter:



        
相关标签:
13条回答
  • 2020-11-30 22:43

    we can format the date on view side in angular is very easy....please check the below example:

    {{dt | date : "dd-MM-yyyy"}}

    0 讨论(0)
  • 2020-11-30 22:47

    Have you seen the Writing directives (short version) section of the documentation?

    HTML

    <div ng-controller="Ctrl2">
          Date format: <input ng-model="format"> <hr/>
          Current time is: <span my-current-time="format"></span>
    </div> 
    

    JS

    function Ctrl2($scope) {
      $scope.format = 'M/d/yy h:mm:ss a';
    }
    
    0 讨论(0)
  • 2020-11-30 22:49

    v.Dt is likely not a Date() object.

    See http://jsfiddle.net/southerd/xG2t8/

    but in your controller:

    scope.v.Dt = Date.parse(scope.v.Dt);
    
    0 讨论(0)
  • 2020-11-30 22:49

    you can get the 'date' filter like this:

    var today = $filter('date')(new Date(),'yyyy-MM-dd HH:mm:ss Z');
    

    This will give you today's date in format you want.

    0 讨论(0)
  • 2020-11-30 22:50

    Inside a controller the format can be filtered by injecting $filter.

    var date = $filter('date')(new Date(),'MMM dd, yyyy');
    
    0 讨论(0)
  • 2020-11-30 22:51

    Here are a few popular examples:

    <div>{{myDate | date:'M/d/yyyy'}}</div> 7/4/2014

    <div>{{myDate | date:'yyyy-MM-dd'}}</div> 2014-07-04

    <div>{{myDate | date:'M/d/yyyy HH:mm:ss'}}</div> 7/4/2014 12:01:59

    0 讨论(0)
提交回复
热议问题