A
C
D
I
R
S
T
Understanding the Hour Function
The Hour function in Power Fx returns the hour of a given time value. It takes a single argument, which can be a time value, a date/time value, or a text value representing a time. The function returns an integer between 0 and 23, representing the hour of the given time value.
Here’s an example of how to use the Hour function in Power Fx:
Hour(Now())
This formula returns the current hour of the day, using the Now function to get the current date/time value.
Converting Time Values
The Hour function is often used in conjunction with other functions to convert time values from one format to another. For example, you might have a text value representing a time in 12-hour format, like “3:30 PM”, and you need to convert it to a 24-hour format for use in a calculation. Here’s how you can do that with the Hour function:
Hour(Text(DateTimeValue("3:30 PM"),"[$-en-US]hh"))
This formula uses the Text function to convert the text value “3:30 PM” to a date/time value, and then uses the Hour function to extract the hour value in 24-hour format.
Similarly, you can use the Hour function to convert a date/time value to a time value. Here’s an example:
Hour(DateAdd(Now(),-1,Days))
This formula subtracts one day from the current date/time value using the DateAdd function, and then uses the Hour function to return the hour value of the resulting time value.
Displaying Time Values
The Hour function can also be used to display time values in your Power Apps app. For example, you might want to display the current time in a label control. Here’s how you can do that with the Hour function:
1. Add a label control to your app screen.
2. Set the Text property of the label control to the following formula:
Text(Hour(Now()),"[$-en-US]hh:mm:ss tt")
This formula uses the Text function to format the hour value of the current time as a string in 12-hour format, with a leading zero if the hour is less than 10. The “tt” specifier adds the “AM” or “PM” designator to the string.
The Hour function in Power Fx is a powerful tool for working with time values in your Power Apps app. Whether you need to perform time-based calculations, convert time values between formats, or display time values in your app, the Hour function can help you get the job done. Use the examples above as a starting point for your own time-related Power Apps development projects.