Home > Net >  utcNow in Power Automate is using incorrect day (`dd`)
utcNow in Power Automate is using incorrect day (`dd`)

Time:10-01

I'm running into a timezone issue when running my Flows on a schedule - and I can't seem to find a solution for it.

My timezone is currently 10:00 from UTC - Sydney, Australia.

For simplicities sake, I have a reoccurrence trigger running at 06:00 (6am local time) and 12:00 (12pm local time).

When I run my Actions - anything that needs a StartTime and an EndTime - I pass in the following parameters:

StartTime Endtime
formatDateTime(utcNow(), 'yyyy-MM-dd"T"22:00:00.0000000Z') formatDateTime( addDays(utcNow(), 1), 'yyyy-MM-dd"T"08:00:00.0000000Z')

The 22:00:00.0000000Z and 08:00:00.0000000Z above translate directly to 8am and 6pm respectively - essentially the entire work day hours.

Issue

When the trigger runs on the first occurrence, the StartTime and the EndTime are outputted to these values:

Tigger time - local UTC date time Start time UTC parameter End time UTC parameter
2022-09-27 06:00 2022-09-26 20:00 2022-09-26T22:00:00.000000Z 2022-09-27T08:00:00.000000Z

When the trigger runs on the second occurrence, the StartTime and the EndTime are outputted to these values:

Tigger time - local UTC date time Start time UTC parameter End time UTC parameter
2022-09-27 12:00 2022-09-27 02:00 2022-09-27T22:00:00.000000Z 2022-09-28T08:00:00.000000Z

Because the UTC date time has ticked over past midnight, the date value (dd) is now the next day due to the addDays(utcNow(), 1) expression.

This causes issues on the scripting of the Flow as when limiting the StartTime and EndTime of other actions, depending on when the Trigger runs is either looking at the local time's "Today" events or "Tomorrow" events.


For example, if I schedule an Out of Office / Automatic reply - and I schedule it to run on the 06:00 trigger, it will set my Automatic Reply to today - local time - 8am to 6pm. However, when it runs at the second trigger, 12:00 it will set my Automatic Reply to tomorrow - local time - 8am to 6pm.

Though this is not how I am using it, you can see it affects what actions do and perform.

Is there a way to ensure that it is always working of the current local date (dd) regardless if the UTC time has ticked over?

CodePudding user response:

Have you tried using the addHours function instead of hard coding the hours in the formatdatetime function?

For the 6 am run I would try for the start time

formatDateTime(addHours(utcNow(), 2), 'yyyy-MM-ddTHH:mm:ss.0000000Z')

And for the end time

formatDateTime(addHours(utcNow(), 12), 'yyyy-MM-ddTHH:mm:ss.0000000Z')

You could also use a check to see which occurence run it is an combine that with the addhours approach.

equals(utcNow('HH'), '20')

CodePudding user response:

It looks like, you want to do your Start time and End time calculations based on your LOCAL date, but you are doing the calculation based on the UTC date and thus get the wrong day, if UTC ticked over.

I guess the solution would be, to do your Start and End time calculations based on the LOCAL time instead of utcNow() and then transform the resulting LOCAL Start and End times into UTC times to be used as Start Time and End Time parameters.

Something to the effect of:

convertFromUtc(utcNow(),' 10:00','yyyy-MM-dd"T"06:00:00.000000')

to get the local start time of the current day and then from there

convertToUtc(localStartTime,' 10:00')

to get the UTC start time with the proper day.

And the same again for the End Time.

  • Related