Trim to Months Returning the Final Day of the Previoud Month Instead of the First Day of the Current Month.
I’m trying to get the first day of the current month based on a User provided date for comparison against a month that I’m holding elsewhere to see if its in the correct period. My first choice was to use trimToMonths(InputDate) and store that in the attribute I have, however when I display the attribute it seems trimToMonths is giving me an incorrect output. When I test months before April of this year I get the last day of the previous month as the output. Example: trimToMonths(3/14/2021) → Output: 2/31/2021 trimToMonths(12/4/2020) → Output: 11/30/2020 This would be relatively easy to handle with an addDays() function or something but once I get past March it starts working correctly. Correct: trimToMonths(4/14/2021) → Output: 4/1/2021 Any idea why this might be happening? I’m using localized dates for all of my dates.
Looks like its a time zone issues. Which timezone are you in? Looks your local time zone has DayLIght saving change starting on 01-Apr.
Try using trimToMonthsUTC , it uses the UTC timezone instead of trimToMonths which uses uses the user’s timezone
In addition to Shekhar’s useful answer the steps for you. In the past I had also issues, so I think it’s useful to see those steps too:
Assume, I have a DateTime attribute ($dt/DateTimeTester), localised, with value current dateTime minus 10 months:
UTC time: 2020-10-03 22:00:00.000
Session time: 2020-10-04 00:00:00.000 +0200
Now, I’m doing a
Which results in:
UTC time: 2020-09-30 22:00:00.000
Session time: 2020-10-01 00:00:00.000 +0200