【问题标题】:angularjs - calculate a date plus one dayangularjs - 计算日期加一天
【发布时间】:2015-11-27 10:23:03
【问题描述】:

我需要在另一个日期后一天得到日期。 我愿意:

$scope.date2.setDate($scope.date1.getDate()+1);

if   
$scope.date1 = 2015-11-27  

then 
$scope.date2 = 2015-11-28

It s ok,
but when 

$scope.date1 = 2015-12-02

 then 
 $scope.date2 = 2015-11-28 (ie tomorrow)

我不明白为什么......

如果有人知道..

【问题讨论】:

  • 你为什么不试试 javascript Date 函数并添加天数 addDays(1) 而不是 +1
  • 另外我会建议你创建 JsFiddle 或 Stack 的内置 sn-p 创建器,这样人们可以看到实际问题并提供更好的解决方案
  • 我在 Javascript 中没有看到任何 addDays
  • 你可以使用 moment.js 进行日期计算
  • getDate() 获取今天的日期。如果你增加它,它将是明天。一切都按预期进行。你在代码中的逻辑是错误的。

标签: javascript angularjs date


【解决方案1】:

试试这个高效 简单 纯JS

var todayDate = new Date();    
console.log(new Date().setDate(todayDate.getDate()+1));

因此您将拥有相同的 Date 类型对象,因此您不需要使用 moment.js

【讨论】:

    【解决方案2】:

    为此 momentjs 使用 moment.js

    var startdate = "2015-12-02";
    var new_date = moment(startdate, "YYYY-MM-DD").add('days', 1);
    var day = new_date.format('DD');
    var month = new_date.format('MM');
    var year = new_date.format('YYYY');
    alert(new_date);
    alert(day + '.' + month + '.' + year);
    

    【讨论】:

      猜你喜欢
      • 2020-09-28
      • 1970-01-01
      • 2010-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      相关资源
      最近更新 更多