title: Angular - Mongoose : Can't manipulate dates
body: <p>I have been searching for a long while for solution, but nothing helped me. </p>
  
  <p>I have an Angular JS app which runs with Mongoose and Express.</p>
  
  <p><strong>I want to store date as object from a simple form.</strong>
  But when I submit my form, dates are stored as String and not as Objects. 
  So I can't do something like :</p>
  
  <pre><code>tache.start.getDate();
  </code></pre>
  
  <p>Here is my form :</p>
  
  <pre><code>&lt;form  name="formTache" ng-submit="addTache(tache)"&gt;
      &lt;div class="form-group row"&gt;
          &lt;div class="col-lg-12"&gt;
              &lt;input name="name" class="form-control" placeholder="Nom" type="text" ng-model="tache.title"/&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="form-group row"&gt;
          &lt;div class="col-lg-12"&gt;
              &lt;input id="date" name="date" class="form-control" placeholder="Date" ng-model="tache.start" type="date"/&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="form-group row"&gt;
           &lt;div class="text-right col-lg-12"&gt;
              &lt;button type="submit" class="btn btn-default"&gt;Ajouter&lt;/button&gt;
           &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <p>Here is my Mongoose Schema :</p>
  
  <pre><code>var restful = require('node-restful');
  var mongoose = restful.mongoose;
  var Schema = mongoose.Schema,
  ObjectId = Schema.ObjectId;
  var tachesSchema = new mongoose.Schema({
      title : String,
      start: {type: Date, default: new Date()},
  });
  var tachesModel = mongoose.model('taches', tachesSchema);
  
  module.exports = restful.model('taches', tachesSchema);
  </code></pre>
  
  <p>Here is my Controller :</p>
  
  <pre><code>angular.module('personaldashboard.tache', [])
    .controller('TachesCtrl', function($scope, Taches, Progress, toaster) {
      $scope.tache = new Taches();
      var refreshTache = function() {
        $scope.taches = Taches.query(); 
        $scope.tache = ''
      }
      refreshTache();
  
      $scope.addTache = function(tache) {
        Taches.save(tache,function(tache){
          refreshTache();
        });
      };
  
      $scope.updateTache = function(tache) {
        tache.$update(function(){
          refreshTache();
        });
      };
  
      $scope.removeTache = function(tache) {
        tache.$delete(function(){
          refreshTache();
        });
      };
  
      $scope.editTache = function(id) {
        $scope.tache = Taches.get({ id: id });
      };  
  
      $scope.deselectTache = function() {
        $scope.tache = ''
      }
      $scope.editTache_Projet = function(tache, projetId) {
        tache.projet = projetId;
        tache.$update(function(){
          refreshTache();
        });
      };
      });
  </code></pre>
  
  <p>Here is what I get : </p>
  
  <pre><code>{ "_id": "58a99df24975ad0104c692b1", "title": "Test", "start": "2017-02-24T23:00:00.000Z" }
  </code></pre>
  
  <p>So why do I get a string like <code>"2017-02-24T23:00:00.000Z"</code> for my date instead of an object whereas my Mongoose schema specify <code>start: {type: Date, default: new Date()}</code> ?</p>
  
  <p>Thanks for your help. </p>
  
  <h1>EDIT</h1>
  
  <p>Thanks to Saurabh Agrawal, I tried to convert the date when submiting in the controller :</p>
  
  <pre><code>$scope.addTache = function(tache) {
    tache.start = new Date(tache.start);
    tache.end = new Date(tache.end);
    Taches.save(tache,function(tache){
      refreshTache();
    });
  };
  </code></pre>
  
  <p>Sadly, this changed nothing :(</p>
  
  <p>I still have a date as string </p>
  
  <pre><code> "2017-02-20T23:00:00.000Z" 
  </code></pre>
  
  <h1>EDIT</h1>
  
  <p>I also tried to add a directive </p>
  
  <pre><code>.directive("formatDate", function(){
    return {
     scope: {ngModel:'='},
      link: function(scope) {
          if (scope.ngModel) {
              scope.ngModel = new Date(scope.ngModel);
          }
      }
    }
  })
  </code></pre>
  
  <p>and call it in my form</p>
  
  <pre><code>&lt;form  name="formTache" ng-submit="addTache(tache)"&gt;
      &lt;div class="form-group row"&gt;
          &lt;div class="col-lg-12"&gt;
              &lt;input name="name" class="form-control" placeholder="Nom" type="text" ng-model="tache.title"/&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="form-group row"&gt;
          &lt;div class="col-lg-12"&gt;
              &lt;input id="date" name="date" class="form-control" placeholder="Date" ng-model="tache.start" type="date" formatDate/&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="form-group row"&gt;
           &lt;div class="text-right col-lg-12"&gt;
              &lt;button type="submit" class="btn btn-default"&gt;Ajouter&lt;/button&gt;
           &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>But nothing changes.</p>
  
  <p>Any other ideas ?</p>