如何在angularjs中定义当前日期

时间:2016-05-31 06:30:33

标签: angularjs json

我是angularjs的新手。我正在尝试定义当前日期,我需要在json请求中使用。

我的 controller.js

(function () {
    'use strict';

    var app = angular.module('app');

app.controller('Controller', function($rootScope, $scope,$window,$http,$q, $filter, Date) {

    var displaynames = $rootScope.displayList;
         console.log(displaynames);

    var current_date = new Date();
            current_date.date = $filter('date')(date[ current_date.date, "yyyy-mm-dd"]);
    console.log(current_date.date);

$scope.displayBirthdays = function(){

         var current_date = new Date();
            current_date.date = $filter('date')(date[ current_date.date, "yyyy-mm-dd"]);

           var birthdays = {
       "json": {
           "request": {
              "servicetype": "21",
              "functiontype": "2021",
              "date": current_date.date
                   }
                }
            }
       }
  });
}) ();

我收到以下错误

angular.js:9997 Error: [$injector:unpr] Unknown provider: DateProvider <- Date
http://errors.angularjs.org/1.2.20/$injector/unpr?p0=DateProvider%20%3C-%20Date
    at https://code.angularjs.org/1.2.20/angular.js:78:12
    at https://code.angularjs.org/1.2.20/angular.js:3754:19
    at Object.getService [as get] (https://code.angularjs.org/1.2.20/angular.js:3882:39)
    at https://code.angularjs.org/1.2.20/angular.js:3759:45
    at getService (https://code.angularjs.org/1.2.20/angular.js:3882:39)
    at invoke (https://code.angularjs.org/1.2.20/angular.js:3909:13)
    at Object.instantiate (https://code.angularjs.org/1.2.20/angular.js:3929:23)
    at https://code.angularjs.org/1.2.20/angular.js:7216:28
    at link (https://code.angularjs.org/1.2.20/angular-route.js:913:26)
    at nodeLinkFn (https://code.angularjs.org/1.2.20/angular.js:6648:13) <div ng-view="" class="ng-scope">

我想仅在json请求中使用日期。我不需要在视图页面中显示它。所以我没有使用$ scope,.... ....

任何人都可以帮助我。

2 个答案:

答案 0 :(得分:2)

从依赖项列表中删除日期

Date是一个javascript对象。所以这将在没有任何依赖注入的情况下可用

app.controller('Controller', function($rootScope, $scope,$window,$http,$q, $filter)

答案 1 :(得分:0)

我建议您在项目中下载momentjs。它非常实用,您可以轻松处理日期。

然后,您甚至可以将日期保存为您想要的任何格式:

moment().format('MMMM Do YYYY, h:mm:ss a'); // May 31st 2016, 8:38:27 am
moment().format('dddd');                    // Tuesday
moment().format("MMM Do YY");               // May 31st 16
moment().format('YYYY [escaped] YYYY');     // 2016 escaped 2016
moment().format();                          // 2016-05-31T08:38:27+02:00

例如,在您的项目中,您可以像这样:

var birthdays = {
       "json": {
           "request": {
              "servicetype": "21",
              "functiontype": "2021",
              "date": moment().format("MMM Do YY");
                   }
                }
            }
相关问题