0

I am new to angularjs. I am trying to define current date, which i need to use in json request.

My 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
                   }
                }
            }
       }
  });
}) ();

I am getting below error

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">

I want to use date only in json request. I no need to display it in view page. So i didnot use $scope,.... ....

Can anyone please help me.

2
  • 2
    You are injecting a Date dependency in your controller, but it doesn't know that provider Commented May 31, 2016 at 6:33
  • Normally you should include only angular's part into dependencies, but not pure js objects, or third-party libs Commented May 31, 2016 at 6:41

2 Answers 2

2

Remove the date from dependency list

Date is a javascript object. So this will be available without any dependency injection

app.controller('Controller', function($rootScope, $scope,$window,$http,$q, $filter)
Sign up to request clarification or add additional context in comments.

Comments

0

I advice you to download momentjs in your project. It's very useful and you can easily deals with dates.

Then, you can even save your date as any format you want :

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

So for example, in your project, you can put like this :

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

1 Comment

Thank you i got it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.