开发者

datejs TimeSpan and TimePeriod problems

开发者 https://www.devze.com 2023-03-13 21:17 出处:网络
I\'m losing the plot with this one.I\'ve added date.j开发者_运维问答s and time.js to my project.

I'm losing the plot with this one. I've added date.j开发者_运维问答s and time.js to my project.

I then having the following test code:

  1  var a = Date.today();
  2  var b = Date.today().next().friday();
  3  var ts = new TimeSpan(b - a);
  4  alert(ts.getDays());

I think everything is correct, yet i receive the following error:

Object doesn't support this property or method at line 4!


Just ensure you are using the current Datejs release in SVN. http://www.datejs.com/svn/.

You can also just get the .days property.

Example

var a = Date.today();
var b = Date.today().next().friday();
var ts = new TimeSpan(b - a);

console.log(ts.days);

EDIT

Within the time.js package of Datejs, along with the TimeSpan class, there is a TimePeriod class which further breaks the difference between two Dates down to include .months and .years.

Here's a full sample using a randomly generated year value. The two console.log values should be the same.

Example

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);

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号