Javascript datejs时间跨度和时间段问题
我失去了这部电影的情节。我已经将date.js和time.js添加到我的项目中 然后我有以下测试代码:Javascript datejs时间跨度和时间段问题,javascript,timespan,datejs,Javascript,Timespan,Datejs,我失去了这部电影的情节。我已经将date.js和time.js添加到我的项目中 然后我有以下测试代码: 1 var a = Date.today(); 2 var b = Date.today().next().friday(); 3 var ts = new TimeSpan(b - a); 4 alert(ts.getDays()); 我认为一切都是正确的,但我收到以下错误: 对象不支持第4行的此属性或方法 只需确保您正在SVN中使用当前的Datejs版本 您也可以
1 var a = Date.today();
2 var b = Date.today().next().friday();
3 var ts = new TimeSpan(b - a);
4 alert(ts.getDays());
我认为一切都是正确的,但我收到以下错误:
对象不支持第4行的此属性或方法
只需确保您正在SVN中使用当前的Datejs版本 您也可以只获取
.days
属性
示例
var a = Date.today();
var b = Date.today().next().friday();
var ts = new TimeSpan(b - a);
console.log(ts.days);
var random = Math.floor(Math.random()*12);
var a = Date.today();
var b = Date.today().add(random).years();
var tp = new TimePeriod(a, b);
console.log('random', random);
console.log('years', tp.years);
编辑
在Datejs的time.js包中,除了TimeSpan
类之外,还有一个TimePeriod
类,它进一步将两个日期之间的差异分解为.month
和.years
下面是使用随机生成的年份
值的完整样本。这两个console.log
值应该相同
示例
var a = Date.today();
var b = Date.today().next().friday();
var ts = new TimeSpan(b - a);
console.log(ts.days);
var random = Math.floor(Math.random()*12);
var a = Date.today();
var b = Date.today().add(random).years();
var tp = new TimePeriod(a, b);
console.log('random', random);
console.log('years', tp.years);
希望这能有所帮助。谢谢你回复我。我设法使它工作起来。我使用的是一个过时的time.js副本。谢谢如果杰弗里·麦吉尔的答案有帮助,请将其标记为已被接受的答案。如何发现不同年份的差异?(例如,2012年1月1日和2014年1月1日=2)得到两年的值并减去它们?是的,已经得到了,只是与时间跨度叠加,并认为年有类似的方法。我添加了一个
时间段
样本,它将准确计算两个日期
对象之间的年
和月
差。