Hi guys. I am trying to create a runtime property that returns a date 10 days prior to today.
The approach I have been using, is to create a DateTime runtime property and added a function to it:
return getDateDaysAgo(10);
function getDateDaysAgo(days) {
const date = new Date();
date.setDate(date.getDate() - days);
return date;
}
When I check the value, it returns “undefined”. It seems that the datatype returned from the function is incompatible with the DataTime datatype on the runtime property.
The reason I need this, is to be able to use the value in a filter on a datasource in our application.
Please advise on a better approach. Any feedback is appreciated
Øystein
Hi
You need to convert the date to an ISOstring when you’re done with all date handling, so that Appfarm can read it.
function getDateDaysAgo(days) {
const date = new Date();
date.setDate(date.getDate() - days);
return date;
}
return getDateDaysAgo(10).toJSON()
You can also use Moment in Create if you prefer: Date and time | Appfarm Documentation
// Erik
Excellent. That seems to do the trick
Thank you.
Øystein
Hi!
Just an additional comment on this one!
If you have not used the Value processor
for the purpose of adding/subtracting time units from a datatime, please check out this article. E.g. for your case, you may set the date to Now
and then apply a Value processor subtracting 10 days.
Hi again and thank you for the update. As I understand it, this approach works well for visual presentations of a date. Is it also possible to use it for evaluations like in IF statements?