Kalaiyarasi M
Kalaiyarasi M

Reputation: 93

How to define current date in angularjs

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.

Upvotes: 0

Views: 176

Answers (2)

Emidomenge
Emidomenge

Reputation: 1524

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");
                   }
                }
            }

Upvotes: 0

Ashish Rajput
Ashish Rajput

Reputation: 1529

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)

Upvotes: 2

Related Questions