1

我正在失去这个情节。我已将 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 行 Object 不支持此属性或方法!

4

1 回答 1

2

只需确保您在 SVN 中使用当前的 Datejs 版本即可。http://www.datejs.com/svn/

您也可以只获得.days财产。

例子

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

console.log(ts.days);

编辑

在 Datejs 的 time.js 包中,除了TimeSpan类之外,还有一个TimePeriod类进一步打破了两个 Date 之间的差异,包括.months.years.

这是使用随机生成的year值的完整示例。这两个console.log值应该相同。

例子

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

希望这可以帮助。

于 2011-11-22T00:01:05.940 回答