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 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. We must convert the UTC date to a user-specific time zone in this scenario.

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 three 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 the Date to local time based on the source time zone. If the time zone is UTC, it must 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 determine whether the date falls on the weekend or not at any timezone.

 

Leave a Reply

Your email address will not be published. Required fields are marked *