计算与json注明日期和现在angularjs的差异

时间:2015-09-21 20:27:02

标签: json angularjs

我正在尝试在json文件中保存日期(最后一次检查)并计算从现在起的时间,因此我可以在需要新检查时向用户显示。 (这是一年一次)

我尝试了一些非常简单的东西,但它似乎无法正常工作

      var currentdate = $scope.user.currentdate;
      var checked = $scope.user.checked;
      var today = new Date();
      var milliseconds_passed  = Date.now() - $scope.user.checked;
      var milliseconds_per_day = 1000*60*60*24;

      // days since last checkup
      var days_passed = Math.floor(milliseconds_passed / milliseconds_per_day);

      // calculate date
      var now = new Date();
      var date_in_milli = now.setDate(now.getDate() - days_passed);

      // not sure why you need to do this because this is effectively the same date as $scope.checked
      $scope.user.currentdate = date_in_milli;

      console.log(checked);
      console.log(today);
      console.log($scope.currentdate);

我的json文件

{
     "id": "11",
     "name": "John Doe",
     "checked": "2015-09-21T20:51:40.946Z",
     "klantnummer": "12341234616"
}

有人可以帮我弄清楚我需要用什么技巧来计算数据,然后向用户展示他需要什么?

控制台日志

2015-09-21T20:51:40.946Z
Mon Sep 21 2015 23:40:19 GMT+0200 (W. Europe Daylight Time)
2015-09-21T20:51:40.946Z
undefined

1 个答案:

答案 0 :(得分:0)

如果$ scope.checked是以毫秒为单位。你可以这样做:

var milliseconds_passed  = Date.now() - $scope.checked;
var milliseconds_per_day = 1000*60*60*24;

// days since last checkup
var days_passed = Math.floor(milliseconds_passed / milliseconds_per_day);

// calculate date
var now = new Date();
var date_in_milli = now.setDate(now.getDate() - days_passed);

// not sure why you need to do this because this is effectively the same date as $scope.checked
$scope.currentdate = date_in_milli;