thaodinh
(Thao Dinh)
1
I am trying to get the last day of the month from the current date as a query parameter. The idea would be
if (the current month is 1,3,5,7,8,10) the last day is 31
else if the current month is 4,6,9,11,12 the last day is 30
else if the current year is leap year (divisible by 4) and the current month is 2 the last day is 29
else the last day is 28
I plan to use #compare to write the expression but I am stuck in how to know if it is leap year.
A nice trick is to take the first day of the next month and then substract one day. That would be like this:
{{dateAdd (join "-" (timestamp "YYYY") (add (timestamp "MM") 1) "01") "-86400000"}}
This wont work in december, so you'll need to do an compare for December and then set YYYY-12-31 in that case.
@thaodinh you could also just do some math do get what you need instead of needing to do a compare. The idea is to:
- Get the first month of the current date. We know the first day of the month for the current date because it would be the current month and then 01.
- After that, you add 32 days to the first date of the current month to get a date into the next month.
- Then you get the first day of the next month.
- Then you subtract 1 day to get the day before the first day of the next month which would be the same as the last day of the current month.
{{dateFormat "YYYY-MM-DD" (dateAdd (dateFormat "YYYY-MM-01" (dateAdd (dateFormat "YYYY-MM-01" (timestamp) "YYYY-MM-DDTHH:mm:ss.SSS[Z]") "2769000000") "YYYY-MM-DDTHH:mm:ss.SSS[Z]") "-86400000") "YYYY-MM-DDTHH:mm:ss.SSS[Z]"}}
thaodinh
(Thao Dinh)
4
thank you both!
@tylerlamparter this works great!