angularjs getdate不工作

时间:2015-04-21 17:42:45

标签: javascript angularjs date

我对javascript和angularjs相当新,所以请耐心等待。尝试与Date对象进行交互时遇到问题。我假设Date是一个javascript对象。但是,当在Date对象上运行.getDate()时,我得到1970年的日期。

我的代码如下所示:

(function() {
  var app = angular.module("app", []);

  var DateController = function($scope) {
        $scope.today = new Date();
        $scope.tomorrow = $scope.today.getDay() +1;
  };

  app.controller("DateController", DateController);
}());

这是非常基本的,但我不明白我为什么要约会。 使用其他一些默认的javascript Date函数时,我也会得到相同的行为。也将不胜感激。

我创造了一个方便的插件:Take me to the plunk!

3 个答案:

答案 0 :(得分:3)

请检查此plunker

你应该使用setDate来改变日期值

你的新控制器应该是这样的:

(function() {
  var app = angular.module("app", []);

  var DateController = function($scope) {
        $scope.today = new Date();
        $scope.tomorrow = new Date();
        $scope.tomorrow = $scope.tomorrow.setDate($scope.tomorrow.getDate() +1);
  };

  app.controller("DateController", DateController);
}());

答案 1 :(得分:1)

在编辑你的插件之后..更多关于Date

(function() {
  var app = angular.module("app", []);

  var DateController = function($scope) {
    $scope.today = new Date();

    // elaborated code for illustration
    var nextDay = new Date($scope.today);
    nextDay.setDate($scope.today.getDate()+1);

    $scope.tomorrow = nextDay;
  };

  app.controller("DateController", DateController);
}());

希望这有帮助。

答案 2 :(得分:0)

This应指向正确的方向,但最终,如果可以,请使用momentjs来操纵日期。