1

With DateJS, you'd add e.g. six months to the current date like this:

Date.today().addMonths(6);

However, I need to add 24 months not to today's date, but to a date which a user has typed into a date field. So the today() should in principle be replaced by something like this.getField('begin_date').value.

The result shall be written into another data form field.

I tried hard, but couldn't make it. Can anyone help me out?

4

2 回答 2

0

如果输入值是日期的文本表示,则首先需要将其转换为Date对象。然后你可以随心所欲地使用它。

DateJS 有一个非常聪明的parse()功能,可以做到这一点,所以你可以这样实现:

Date.parse(this.getField('begin_date').value).addMonths(24)

当需要特定的日期格式时,如DD.MM.YYYY欧洲常用的,您可以使用parseExact()并指定格式。像这样:

Date.parseExact(dateToParse, 'dd.MM.yyyy')  // leading zeroes required; parses 01.04.2014 but not 1.4.2014
Date.parseExact(dateToParse, 'd.M.yyyy')  // leading zeroes not required; parses both 01.04.2014 and 1.4.2014
于 2014-01-12T10:30:47.440 回答
0

这是我为我的问题找到的解决方案,也使用了 DateJS:

start = this.getField('begin_date').value;
var d1 = util.scand("dd.mm.yyyy", start);
var myDate = new Date(d1);
result = myDate.addMonths(24);

这工作得很好,也跨越闰年,除了 2014/2018/2022 年 2 月 28 日 ... ; 结果将是 2016/2020/2024 年 2 月 28 日...而不是 2016/2020/2024 年 2 月 29 日...在这些情况下,由用户决定接受 28 日或手动更改日期到 29 日。

于 2014-01-13T07:31:34.897 回答