# Date handling using Javascript

**URL:** <https://community.appfarm.io/t/date-handling-using-javascript/1570>\
**Category:** Ask the community\
**Tags:** runtime\
**Created:** [December 19, 2024, 12:11am UTC](https://community.appfarm.io/t/date-handling-using-javascript/1570 "2024-12-19T00:11:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oystein\_Selbekk](https://dub1.discourse-cdn.com/flex013/user_avatar/community.appfarm.io/oystein_selbekk/32/1677_2.png) [@Oystein\_Selbekk](https://community.appfarm.io/u/Oystein_Selbekk)\
**Post date:** [December 19, 2024, 12:11am UTC](https://community.appfarm.io/t/date-handling-using-javascript/1570/1 "2024-12-19T00:11:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ErikAKSkallevold](https://dub1.discourse-cdn.com/flex013/user_avatar/community.appfarm.io/erikakskallevold/32/1378_2.png) [@ErikAKSkallevold](https://community.appfarm.io/u/ErikAKSkallevold)\
**Post date:** [December 19, 2024, 6:09am UTC](https://community.appfarm.io/t/date-handling-using-javascript/1570/2 "2024-12-19T06:09:10Z")

</div>

Hi

You need to convert the date to an ISOstring when you’re done with all date handling, so that Appfarm can read it.

```auto
function getDateDaysAgo(days) {
    const date = new Date();
    date.setDate(date.getDate() - days);
    return date;
}

return getDateDaysAgo(10).toJSON()

```

> **[Date.prototype.toJSON() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON)**
>
> The toJSON() method of Date instances returns a string representing this date in the same ISO format as toISOString().

You can also use Moment in Create if you prefer: [Date and time | Appfarm Documentation](https://docs.appfarm.io/reference/platform-concepts/date-and-time#function-editor-and-moment.js)

// Erik

---

<div class="post-metadata">

**Author:** ![Oystein\_Selbekk](https://dub1.discourse-cdn.com/flex013/user_avatar/community.appfarm.io/oystein_selbekk/32/1677_2.png) [@Oystein\_Selbekk](https://community.appfarm.io/u/Oystein_Selbekk)\
**Post date:** [December 20, 2024, 12:19pm UTC](https://community.appfarm.io/t/date-handling-using-javascript/1570/3 "2024-12-20T12:19:23Z")

</div>

Excellent. That seems to do the trick 🙂  
Thank you.

Øystein

---

<div class="post-metadata">

**Author:** ![kristian](https://dub1.discourse-cdn.com/flex013/user_avatar/community.appfarm.io/kristian/32/58_2.png) [@kristian](https://community.appfarm.io/u/kristian)\
**Post date:** [December 23, 2024, 8:37am UTC](https://community.appfarm.io/t/date-handling-using-javascript/1570/4 "2024-12-23T08:37:42Z")

</div>

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](https://docs.appfarm.io/reference/platform-concepts/date-and-time#value-processor). E.g. for your case, you may set the date to `Now` and then apply a Value processor subtracting 10 days.

---

<div class="post-metadata">

**Author:** ![Oystein\_Selbekk](https://dub1.discourse-cdn.com/flex013/user_avatar/community.appfarm.io/oystein_selbekk/32/1677_2.png) [@Oystein\_Selbekk](https://community.appfarm.io/u/Oystein_Selbekk)\
**Post date:** [December 24, 2024, 11:08am UTC](https://community.appfarm.io/t/date-handling-using-javascript/1570/5 "2024-12-24T11:08:43Z")

</div>

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?
