Not for the first time I find myself struggling to work out how to apply a script method. In the attached example I am trying, and failing, to add 1 hour to a modified date. Could some kind soul please point me in the right direction.
Regards, AB
Not for the first time I find myself struggling to work out how to apply a script method. In the attached example I am trying, and failing, to add 1 hour to a modified date. Could some kind soul please point me in the right direction.
Regards, AB
Date.Add modifies the date object it is used on, and does not return anything:
Instead of:
new_date_obj = date_obj.Add(1,"h");
Try:
new_date_obj = date_obj.Clone();
new_date_obj.Add(1,"h");
(BTW, it's good to paste the text version of code for questions like this, so people can quickly try it out without having to type everything from a screenshot.)
Good point re text vs screen shot. Your Add code sample using a clone date object works fine. Given that the original date_obj variable is also a date object I would expect the second Add to work but it doesn't.
Regards, AB
var src = DOpus.listers.lastactive.activetab;
var n = src.selected.count;
var date_obj;
var new_date_obj;
DOpus.clearoutput;
for (var i = 0; i < n; i++){
date_obj = src.selected(i).modify;
DOpus.output(date_obj);
new_date_obj = date_obj.Clone();
new_date_obj.Add(1,"h"); // This works on the clone
DOpus.output(new_date_obj);
date_obj.Add(1,"h"); // Not clear to me why this doesn't work
DOpus.output(date_obj);
}
Output:
21/10/2016 12:10:28
21/10/2016 13:10:28
Error at line 12, position 2
A method was called unexpectedly (0x8000ffff)
Ah, that is because the Date objects Item returns are all set as read-only, to avoid accidentally modifying them (since it would change what item.modified returned if you called it a second time).
Thanks. Makes sense.
Regards, AB