Datetime calculation in macro

Wei Wang asked on September 1, 2023 17:09

Hi, I can't make this datetime calculation in a dropdonw list control in a form to work. It always shows the first option regardless of the date interval greater or less then 10 days.

SELECT TicketPayUrl, TicketPayUrl FROM customtable_ParkingTicketPayURL {% if(ToTimeSpan(CurrentDateTime - TicketDate).Days <= 10){"WHERE ItemID = 1"} #%} {% if(ToTimeSpan(CurrentDateTime - TicketDate).Days > 10){"WHERE ItemID = 2"} #%}

I am in Kentico 11 portal engine.

Thanks, Wei Wang

Recent Answers

Dmitry Bastron answered on September 1, 2023 17:48 (last edited on September 1, 2023 17:50)

Hi Wei,

Feels like something is wrong with the calculation. Is TicketDate of type DateTime?

To debug this in your context just withdraw sequentially

  • CurrentDateTime
  • TicketDate
  • CurrentDateTime - TicketDate
  • ToTimeSpan(CurrentDateTime - TicketDate)
  • ToTimeSpan(CurrentDateTime - TicketDate).Days

This will help you to understand where the error is and then fix it and implement the condition.

Also I would recommend using else statement instead of 2 if conditions.

0 votesVote for this answer Mark as a Correct answer

Wei Wang answered on September 1, 2023 21:10

Thanks. Yes, TicketDate is DateTime. Not sure why ToTimeSpan function isn't work here. It's not returning the data differences in days. All I want is the date differences between two dates.

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on September 3, 2023 10:35

Is the context of the TicketDate is available? What does the macro and the SQL debug say? I would also test the macro in System -> Macros -> Console to see what values are returned.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.