Cast Invalid Error

Hi All,

I found some strange behavior with the below expressions related to date and time.

{
  /* All these worked */
  now() + second() * 62,
  now() + minute() * 5,
  now() + hour() * 5,
  now() + day() * 5,
  
  /* These didn't work */
  now() + month() * 5,
  now() + year() * 5
}

Can someone please explain why the last 2 statements don't work, and throw cast invalid error?

  Discussion posts and replies are publicly visible

Parents Reply
  • 0
    Certified Lead Developer
    in reply to vamsi krishna

    In order to use tointervalds() function here, you need to calculate the no of days in upcoming 5 (or required number of) months, and then replace 5 with that value. now() returns date time and it only can add/subtract in days so you need to convert year/month into days so that addition/subtraction can happen with now(). 

    As an alternative, if your usecase is to add a specific number of months/years to a date I suggest you to take a look at plugin Date and Time Utilities in the app market. Functins addmonths() & addyears() can help you here!

    {
      addseconds(now(), 62),
      addminutes(now(), 5),
      addhours(now(), 5),
      adddays(now(), 5),
      addmonths(now(), 5),
      addyears(now(), 5)
    }

Children
No Data