Angularjs - display current date

后端 未结 11 1146
醉酒成梦
醉酒成梦 2020-12-04 08:06

I got a view in angularjs and I\'m just trying to display the current date(formatted). I thought something like {{Date.now() | date:\'yyyy-MM-dd\'}}

相关标签:
11条回答
  • 2020-12-04 08:45

    You can use moment() and format() functions in AngularJS.

    Controller:

    var app = angular.module('demoApp', []);   
    app.controller( 'demoCtrl', ['$scope', '$moment' function($scope , $moment) {
    $scope.date = $moment().format('MM/DD/YYYY');
    }]);
    

    View:

    <div ng-app="demoApp">
      <div ng-controller="demoCtrl">
        {{date}}
      </div>
    </div>
    
    0 讨论(0)
  • 2020-12-04 08:49

    Another way of doing is: In Controller, create a variable to hold the current date as shown below:

    var eventsApp = angular.module("eventsApp", []);
    eventsApp.controller("EventController", function EventController($scope) 
    {
    
     $scope.myDate = Date.now();
    
    });
    

    In HTML view,

    <!DOCTYPE html>
    <html ng-app="eventsApp">
    <head>
        <meta charset="utf-8" />
       <title></title>
       <script src="lib/angular/angular.js"></script>
    </head>
    <body>
    <div ng-controller="EventController">
    <span>{{myDate | date : 'yyyy-MM-dd'}}</span>
    </div>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-04 08:52

    A solution similar to the one of @Nick G. by using filter, but make the parameter meaningful:

    Implement an filter called relativedate which calculate the date relative to current date by the given parameter as diff. As a result, (0 | relativedate) means today and (1 | relativedate) means tomorrow.

    .filter('relativedate', ['$filter', function ($filter) {
      return function (rel, format) {
        let date = new Date();
        date.setDate(date.getDate() + rel);
        return $filter('date')(date, format || 'yyyy-MM-dd')
      };
    }]);
    

    and your html:

    <div ng-app="myApp">
        <div>Yesterday: {{-1 | relativedate}}</div>
        <div>Today: {{0 | relativedate}}</div>
        <div>Tomorrow: {{1 | relativedate}}</div>
    </div>
    
    0 讨论(0)
  • 2020-12-04 08:53

    View

    <div ng-app="myapp">
    {{AssignedDate.now() | date:'yyyy-MM-dd HH:mm:ss'}}
    </div>
    

    Controller

    var app = angular.module('myapp',[])

    app.run(function($rootScope){
        $rootScope.AssignedDate = Date;
    })
    
    0 讨论(0)
  • 2020-12-04 08:54

    You have to create a date object in your controller first:

    controller:

    function Ctrl($scope)
    {
        $scope.date = new Date();
    }
    

    view:

    <div ng-app ng-controller="Ctrl">
        {{date | date:'yyyy-MM-dd'}}
    </div>
    

    JSFiddle example

    Angular Date Filter Ref

    0 讨论(0)
  • 2020-12-04 08:55

    Just my 2 cents in case someone stumble upon this :)

    What I am suggesting here will have the same result as the current answer however it has been recommended to write your controller the way that I have mentioned here.

    Reference scroll to the first "Note" (Sorry it doesn't have anchor)

    Here is the recommended way:

    Controller:

    var app = angular.module('myApp', []);   
    app.controller( 'MyCtrl', ['$scope', function($scope) {
        $scope.date = new Date();
    }]);
    

    View:

    <div ng-app="myApp">
      <div ng-controller="MyCtrl">
        {{date | date:'yyyy-MM-dd'}}
      </div>
    </div>
    
    0 讨论(0)
提交回复
热议问题