Trim to Months Returning the Final Day of the Previoud Month Instead of the First Day of the Current Month.

1
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.
asked
2 answers
2

Hi Chase,

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

Regards,

Shekhar

answered
0

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
     
    trimToMonths($dt/DateTimeTester)

    Which results in: 

    UTC time: 2020-09-30 22:00:00.000
    Session time: 2020-10-01 00:00:00.000 +0200

     
  • Now, I’m doing a
     
    formatDateTime($dt/DateTimeTester, 'yyyy-MM-dd')

    which results in:

    2020-10-01
     
  • Now, I’m doing a
     
    formatDateTimeUTC($dt/DateTimeTester, 'yyyy-MM-dd')

    which results in:

    2020-09-30

 

You see the difference between UTC time and session time (localised).

answered