Power Automate: Find the Given Date is Weekend for User Time Zone (Fri 6 PM to Mon 8 AM)

This blog will discuss finding the given date that falls on the weekend or not with the time zone. This blog extension to another blog but no time zone.

Background:

I have a use case not to send email notifications on weekends when a record is created. It means no email should send on weekends. For their business, the weekend starts Friday at 6 PM and ends Monday at 8 AM.

Requirement:

Find the given date as a weekend based on the time zone. In our example, the weekend starts Friday at 6 PM and ends Monday at 8 AM. Normally data (DateTime) is stored as UTC, and the requirement is not to send a notification to that specific user. In this scenario, we must convert the UTC date to a user-specific time zone.

In this example, the Target time zone is a variable we can retrieve from some data source (table).

Steps:

    • Get the time zone for input Date
    • Convert to the Target time zone
    • Find weekend or not

Overall Actions:

Explanations of each Step/Action:

Initialize 3 variables:

    1. Test DateTime (Input to Test)
    2. Source Time Zone
    3. Target Time Zone

Convert to Local for Testing:

if(equals(variables(‘Source Time Zone’),’UTC’), convertFromUtc(variables(‘Test DateTime’),variables(‘Target Time Zone’)),variables(‘Test DateTime’))

Compose DateTime:

Convert Date to local time based on source time zone. If the time zone is UTC, then needs to convert to the target (Local) time zone.

formatDateTime(if(not(equals( variables(‘Source Time Zone’),’UTC’)), variables(‘Test DateTime’), convertFromUtc(variables(‘Test DateTime’), variables(‘Target Time Zone’))))

Compose DayOfWeek:

dayOfWeek(outputs(‘Compose_DateTime’))

Find Weekend or Not:

or(
equals(outputs(‘DayofWeek’),6),
equals(outputs(‘DayofWeek’),0),
and(equals(outputs(‘DayofWeek’),5), greaterOrEquals(int(formatDateTime(outputs(‘Compose_DateTime’),’HH’)),18)),
and(equals(outputs(‘DayofWeek’),1), less(int(formatDateTime(outputs(‘Compose_DateTime’),’HH’)),8))
)

Conclusion:

We can find whether the date falls on the weekend or not for any timezone.

 

Leave a Reply

Your email address will not be published.