# hour Script and chart function

This function returns an integer representing the hour when the fraction of the expression is interpreted as a time according to the standard number interpretation.

Syntax:

hour(expression)

Return data type: integer

## Regional settings

Unless otherwise specified, the examples in this topic use the following date format: MM/DD/YYYY. The date format is specified in the `SET DateFormat` statement in your data load script. The default date formatting may be different in your system, due to your regional settings and other factors. You can change the formats in the examples below to suit your requirements. Or you can change the formats in your load script to match these examples.

Default regional settings in applications are based on the regional system settings of the computer or server where Qlik Sense is installed. If the Qlik Sense server you are accessing is set to Sweden, the Data load editor will use Swedish regional settings for dates, time, and currency. These regional format settings are not related to the language displayed in the Qlik Sense user interface. Qlik Sense will be displayed in the same language as the browser you are using.

Function examples

| Example | Result |
| --- | --- |
| `hour(<br>'09:14:36' )` | The text string supplied is implicitly converted to a timestamp as it matches the timestamp format defined in the TimestampFormat variable. The expression returns 9. |
| `hour(<br>'0.5555' )` | The expression returns 13 ( Because 0.5555 = 13:19:55 ). |

## Example 1 – Variable (script)

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/DateAndTimeFunctions/hour.htm)

#### Load script and results

Overview

Open the Data load editor and add the load script below to a new tab.

The load script contains:

* A dataset containing transactions by timestamp

* The default `TimeStamp` system variable `(M/D/YYYY h:mm:ss[.fff] TT)`

Create a field '`hour`', calculating when purchases took place.

Load script

```
SET TimestampFormat='M/D/YYYY h:mm:ss[.fff] TT';
						
Transactions: 			
  Load 
    *, 
    hour(date) as hour 
    ; 
Load 
* 
Inline  
[ 
id,date,amount 
9497,'2022-01-05 19:04:57',47.25, 
9498,'2022-01-03 14:21:53',51.75, 
9499,'2022-01-03 05:40:49',73.53, 
9500,'2022-01-04 18:49:38',15.35, 
9501,'2022-01-01 22:10:22',31.43, 
9502,'2022-01-05 19:34:46',13.24, 
9503,'2022-01-04 22:58:34',74.34, 
9504,'2022-01-06 11:29:38',50.00, 
9505,'2022-01-02 08:35:54',36.34, 
9506,'2022-01-06 08:49:09',74.23 
]; 
```

Results

Load the data and open a sheet. Create a new table and add these fields as dimensions:

* `date`

* `hour`

Results table

| date | hour |
| --- | --- |
| 2022-01-01 22:10:22 | 22 |
| 2022-01-02 08:35:54 | 8 |
| 2022-01-03 05:40:49 | 5 |
| 2022-01-03 14:21:53 | 14 |
| 2022-01-04 18:49:38 | 18 |
| 2022-01-04 22:58:34 | 22 |
| 2022-01-05 19:04:57 | 19 |
| 2022-01-05 19:34:46 | 19 |
| 2022-01-06 08:49:09 | 8 |
| 2022-01-06 11:29:38 | 11 |

The values in the hour field are created by using the `hour()` function and passing the date as the expression in the preceding load statement.

## Example 2 – Chart object (chart)

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/DateAndTimeFunctions/hour.htm)

#### Load script and chart expression

Overview

Open the Data load editor and add the load script below to a new tab.

The load script contains:

* The same dataset from the first example.

* The default `TimeStamp` system variable `(M/D/YYYY h:mm:ss[.fff] TT)`.

However, in this example, the dataset, unchanged, is loaded into the application. The ‘`hour`’ values are calculated via a measure in a chart object.

Load Script

```
SET TimestampFormat='M/D/YYYY h:mm:ss[.fff] TT'; 
			 
Transactions: 
Load 
* 
Inline  
[ 
id,date,amount 
9497,'2022-01-05 19:04:57',47.25, 
9498,'2022-01-03 14:21:53',51.75, 
9499,'2022-01-03 05:40:49',73.53, 
9500,'2022-01-04 18:49:38',15.35, 
9501,'2022-01-01 22:10:22',31.43, 
9502,'2022-01-05 19:34:46',13.24, 
9503,'2022-01-04 22:58:34',74.34, 
9504,'2022-01-06 11:29:38',50.00, 
9505,'2022-01-02 08:35:54',36.34, 
9506,'2022-01-06 08:49:09',74.23 
]; 
				
```

Results

Load the data and open a sheet. Create a new table and add this field as a dimension: `date`.

To calculate the ‘`hour`’, create the following measure:

`=hour(date)`

Results table

| due\_date | =hour(date) |
| --- | --- |
| 2022-01-01 22:10:22 | 22 |
| 2022-01-02 08:35:54 | 8 |
| 2022-01-03 05:40:49 | 5 |
| 2022-01-03 14:21:53 | 14 |
| 2022-01-04 18:49:38 | 18 |
| 2022-01-04 22:58:34 | 22 |
| 2022-01-05 19:04:57 | 19 |
| 2022-01-05 19:34:46 | 19 |
| 2022-01-06 08:49:09 | 8 |
| 2022-01-06 11:29:38 | 11 |

The values for ‘`hour`’ are created by using the `hour()` function and passing the date as the expression in a measure for the chart object.