var d1 = new Date('01/16/2013');
var d2 = new Date('02/26/2013');
var milliseconds = d2-d1;
var seconds = milliseconds / 1000;
var minutes = seconds / 60;
var hours = minutes / 60;
var days = hours / 24;
使用其中一个日期库(如moment.js):
var d1 = moment("01/16/2013");
var d2 = moment("02/26/2013");
var days = moment.duration(d2.diff(d1)).asDays();
var Date1 = 08/16/2004;
var Date2= 10/24/2005;
var timestamp1 = new Date(Date1).getTime();
var timestamp2 = new Date(Date2).getTime();
var diff = timestamp1 - timestamp2
var newDate = new Date (diff);
<script src="http://code.angularjs.org/0.10.4/angular-0.10.4.min.js" ng:autobind></script>
<div ng:controller="ComputedPropertiesCtrl">
first date <input ng:model="firstdate"> second date <input ng:model="seconddate"> difference {{diff}}
<div>when the difference is greater than 10 color changes to green</div>
<div>eg set the second date to 15/01/2013</div>
<div style="background-color:{{col}};"> State </div>
</div>
JS系统
function ComputedPropertiesCtrl() {
var scope = this;
scope.firstdate = '01/01/2013';
scope.seconddate = '10/01/2013';
scope.data_before = [];
scope.differenceInDays = function() {
var dt1 = scope.firstdate.split('/'),
dt2 = scope.seconddate.split('/'),
one = new Date(dt1[2], dt1[1]-1, dt1[0]),
two = new Date(dt2[2], dt2[1]-1, dt2[0]);
var millisecondsPerDay = 1000 * 60 * 60 * 24;
var millisBetween = two.getTime() - one.getTime();
var days = millisBetween / millisecondsPerDay;
return Math.floor(days);
};
scope.color = function() {
return (scope.differenceInDays() > 10) ? 'green' : 'red';
};
scope.$watch('[firstdate, seconddate]', function(currScope,newVal,oldVal) {
scope.data_before = oldVal;
scope.diff = scope.differenceInDays();
});
scope.$watch('[firstdate, seconddate]', function(currScope,newVal,oldVal) {
scope.data_before = oldVal;
scope.col = scope.color();
});
}
// Returns the days between a & b date objects...
function dateDiffInDays(a, b) {
var _MS_PER_DAY = 1000 * 60 * 60 * 24;
// Discard the time and time-zone information.
var utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
var utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());
return Math.floor((utc2 - utc1) / _MS_PER_DAY);
}
// Calculate how many days between now and an event...
function daysTill(e) {
var eventE = new Date(e);
var today = new Date();
return dateDiffInDays(today, eventE);
}
var parsedServerOutTime = moment(serverOutTime, "HH:mm:ss");
var parsedServerInTime = moment(serverInTime, "HH:mm:ss");
var milliseconds= parsedServerOutTime.diff(parsedServerInTime) //default milliseconds
var days = moment.duration(parsedServerOutTime .diff(parsedServerInTime )).asDays();// For days
angular.module('myApp', [])
.controller('myCtrl', ['$scope', function($scope) {
$scope.myFunc = function() {
var reqDate = $scope.reqDate;
let cDate = new Date();
let reqSent = new Date(reqDate);
var from = Date.UTC(cDate.getFullYear(), cDate.getMonth(), cDate.getDate());
var to = Date.UTC(reqSent.getFullYear(), reqSent.getMonth(), reqSent.getDate());
let day = Math.floor((from - to) /(1000 * 60 * 60 * 24));
$scope.day = day;
};
}]);
8条答案
按热度按时间mgdq6dx11#
基本的javascript方式:
使用其中一个日期库(如moment.js):
stszievb2#
您只需将日期转换为时间戳,然后相减即可。
e4yzc0pl3#
您可以使用angular-moment计算差异,使用amDifference过滤器:
获取两个日期之间的差值(以毫秒为单位)。参数为date、units和usePrecision。Date默认为当前日期。示例:
wmtdaxz34#
我也是angularjs的新手,但是你不会通过javascript视图模型提供属性来解决这个问题吗?
例如,有一个基于日期字段更改的 * style * 字段(即,如果差异为10天,则style返回style 1),并希望通过angularjs绑定将更新传播到屏幕。
我认为正确的术语是 * 计算属性 * 或 * 计算属性 *
不确定这是否是你要找的,但是看看fiddle的例子,计算日期差异和改变样式都是基于视图模型的属性(作用域)
scope.diff
和scope.col
是要绑定到的2个属性http://jsfiddle.net/chrismoutray/wfjv6/
超文本标记语言
JS系统
中央支助系统
w80xi6nr5#
这是可行的,这里有2个完美的javascript日期函数,你永远不应该没有...
doinxwow6#
JavaScript库非常有用且易于用途:
asWeeks();
asMonths();
asYears();
等有关详细信息,请查看http://momentjs.com/docs/ddarikpa7#
我试着用这个来计算总天数。天数是从日期到日期计算的。所以,默认情况下,到日期是指当前日期。
p5cysglq8#
我尝试了下面的一个,它为我工作
选定的日期是从日历中选定的日期,它来自父范围。第二个日期将是今天的日期。稍后我将使用时间差查找差异。
我使用parseInt是因为我只想返回整数值
希望这能管用