Dates - The easiest way to return the day name for a date
January 30. 2021
With Power Apps, it isn't too difficult to return the day name that corresponds to a date. There are various methods to carry this out, and here's a technique that I often see in forum posts.
This technique relies on the Weekday function. The Weekday function takes an input date and returns a number from 1 to 7 that indicates the day number. The Switch function returns a string that corresponds to the weekday number. This technique however, is more difficult than it needs to be.
The easier way to accomplish this
Here's an easier way to accomplish this. With a single line of formula, we can return the day name with syntax that looks like this:
This uses the text function to format the input date with a
custom format string. The ddd placeholder returns a three character
abbreviation (ie, Sun to Sat), whereas the dddd placeholder displays the
full name (ie, Sunday to Saturday).
What other benefits are there to this?
Not only is this technique much more straightforward, it also displays the day name in the language of the current user.
As we can see in the screenshot beneath, the formula displays the day name in Spanish, on a Spanish machine.
What if we always want to return the English version?
If we prefer not to return a localised version of the day name and prefer instead to always return day name in English, we can pass the "en" language code like so:
Text(Now(), "dddd", "en")
Taking the same example from above, the same formula now returns the English day name when we pass the language code "en".
Extending this technique further, we can substitute "en" with any of the supported locale strings. Here, we display the Russian version by passing the language code "ru".
Note - this technqiue works only when input value is of data type date/datetime
One reason why app builders can struggle with date formatting strings is that it works only with input values that are of type date or datetime. If we supply string representation of a date, the method will fail. In this scenario, we need to convert the string to a date using the DateValue function.
Whenever we call the DateValue function, it's good practice to specify the format of the input string to avoid any inaccurate conversions that can arise due to the language of the end user.
Let's suppose we want to display the day name that corresponds to the input string "06/01/2021" in UK format (6th January 2021). Here's the formula that we would use.
Text(DateValue("06/01/2021", "en-gb"), "ddd")
In contrast, if we want to display the day name that corresponds to
the input string "06/01/2021" in US format (1st June 2021), we would pass the locale string "en-us" like so.
Text(DateValue("06/01/2021", "en-us"), "ddd")
The easiest way to display the day name that corresponds to a date is to call the Text function and pass the format string "ddd" or "dddd". This technique is localisable, and is simpler than writing a formula that uses the Weekday/Switch functions.