# indaytotime 脚本和图表功能

此函数用于返回 True，如果 timestamp 位于包含 base\_timestamp 为止以及包括 base\_timestamp 精确毫秒的日子部分以内。

语法：

InDayToTime (timestamp, base\_timestamp, period\_no[, day\_start])

`indaytotime()` 函数根据时间戳值在一天中的某段时间内出现的时间返回布尔值结果。该段的开始边界是一天的开始，默认设置为午夜；可以通过 `indaytotime()` 函数的 `day_start` 参数修改一天的开始。日段的结束边界由函数的 `base_timestamp` 参数确定。

`indaytotime` 函数的图表。

`indaytotime` 函数的图表。

`indaytotime`indaytotime 函数的图表。 ![显示 indaytotime 函数变量之间关系的图表。](https://help.qlik.com/zh-CN/cloud-services/Subsystems/Hub/Content/Resources/Images/scripting-functions-indaytotime-diagram-overview.png)

适合使用 indaytotime 的情况

`indaytotime()` 函数返回布尔结果。通常，这种类型的函数将用作 `if expression` 中的条件。`indaytotime()` 函数返回一个聚合或计算，具体取决于时间戳是否发生在截至并包括基本时间戳时间的时间段内。

例如，该 `indaytotime()` 函数可用于显示迄今为止已进行的演出的门票销售总额。

返回数据类型： 布尔值

在 Qlik Sense 中，布尔 true 值由 -1 表示，false 值由 0 表示。

参数

| 参数 | 描述 |
| --- | --- |
| ` timestamp  ` | 想要用来与 `base_timestamp` 进行比较的日期和时间。 |
| ` base_timestamp  ` | 日期和时间用于计算时间戳的值。 |
| ` period_no  ` | 该天可通过 `period_no` 偏移。`period_no` 为整数，其中值 0 表示该天包含 `base_timestamp`。`period_no` 为负数表示前几天，正数表示随后的几天。 |
| ` day_start  ` | （可选）如果不想从每一日的午夜开始处理，可在 `day_start` 中指定一个偏移作为某日内时间的小数。例如，使用 0.125 表示凌晨 3 点。 |

## 区域设置

除非另有规定，本主题中的示例使用以下日期格式：MM/DD/YYYY。日期格式已经在数据加载脚本中的 `SET DateFormat` 语句中指定。由于区域设置和其他因素，系统中的默认日期格式可能有所不同。您可以更改以下示例中的格式以满足您的要求。或者，您可以更改加载脚本中的格式以匹配这些示例。有关更多信息，请参阅[修改应用程序和脚本的区域设置](https://help.qlik.com/zh-CN/cloud-services/Subsystems/Hub/Content/Sense_Hub/Apps/regional-settings-in-apps.htm)。

应用程序中的默认区域设置基于用户配置文件。这些区域格式设置与 Qlik Cloud 用户界面中显示的语言无关。Qlik Cloud 将以与您使用的浏览器相同的语言显示。

如果你是应用程序创建者，你可以为自己创建的应用程序设置默认区域。有关更多信息，请参阅[设置 Qlik Cloud Analytics 中创建应用程序和脚本所用的首选区域设置](https://help.qlik.com/zh-CN/cloud-services/Subsystems/Hub/Content/Sense_Hub/UserInterface/Languages.htm#Setting)。

函数示例

| 示例 | 结果 |
| --- | --- |
| `indaytotime` ('01/12/2006 12:23:00 PM', '01/12/2006 11:59:00 PM', 0) | 返回 True |
| `indaytotime` ('01/12/2006 12:23:00 PM', '01/12/2006 12:00:00 AM', 0) | 返回 False |
| `indaytotime` '01/11/2006 12:23:00 PM', '01/12/2006 11:59:00 PM', -1) | 返回 True |

## 示例 1 – 没有其他参数

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

#### 加载脚本和结果

概述

打开 数据加载编辑器，并将下面的加载脚本添加到新选项卡。

加载脚本包含：

* 包含 1 月 4 日至 5 日期间的一组事务的数据集被载入名为 'Transactions' 的表格。

* 以 `TimeStamp` 系统变量 `(M/D/YYYY h:mm:ss[.fff] TT)` 格式提供的日期字段。

* 一种前置 Load，其中包含设置为 `'in_day_to_time'` 的 `indaytotime()` 函数，该字段确定每个事务是否在早上 9:00 之前发生。

加载脚本

```
SET TimestampFormat='M/D/YYYY h:mm:ss[.fff] TT';

Transactions:
    Load
        *,
        indaytotime(date,'01/05/2022 9:00:00 AM',0) as in_day_to_time
        ;
Load
*
Inline 
[
id,date,amount
8188,'01/04/2022 3:41:54 AM',25.66
8189,'01/04/2022 4:19:43 AM',87.21
8190,'01/04/2022 4:53:47 AM',53.80
8191,'01/04/2022 8:38:53 AM',69.98
8192,'01/04/2022 10:37:52 AM',57.42
8193,'01/04/2022 1:54:10 PM',45.89
8194,'01/04/2022 5:53:23 PM',82.77
8195,'01/04/2022 8:13:26 PM',36.23
8196,'01/04/2022 10:00:49 PM',76.11
8197,'01/05/2022 7:45:37 AM',82.06
8198,'01/05/2022 8:44:36 AM',17.17
8199,'01/05/2022 11:26:08 AM',40.39
8200,'01/05/2022 6:43:08 PM',37.23
8201,'01/05/2022 10:54:10 PM',88.27
8202,'01/05/2022 11:09:09 PM',95.93
];
```

结果

加载数据并打开工作表。创建新表并将这些字段添加为维度：

* `date`

* `in_day_to_time`

结果表

| 日期 | in\_day\_to\_time |
| --- | --- |
| 01/04/2022 3:41:54 AM | 0 |
| 01/04/2022 4:19:43 AM | 0 |
| 01/04/2022 04:53:47 AM | 0 |
| 01/04/2022 8:38:53 AM | 0 |
| 01/04/2022 10:37:52 AM | 0 |
| 01/04/2022 1:54:10 PM | 0 |
| 01/04/2022 5:53:23 PM | 0 |
| 01/04/2022 8:13:26 PM | 0 |
| 01/04/2022 10:00:49 PM | 0 |
| 01/05/2022 7:45:37 AM | -1 |
| 01/05/2022 8:44:36 AM | -1 |
| 01/05/2022 11:26:08 AM | 0 |
| 01/05/2022 6:43:08 PM | 0 |
| 01/05/2022 10:54:10 PM | 0 |
| 01/05/2022 11:09:09 PM | 0 |

示例 1 上午 9:00 限制的 `indaytotime` 函数图。

示例 1 上午 9:00 限制的 `indaytotime` 函数图。

示例 1 上午 9:00 限制的 `indaytotime`indaytotime 函数图。 ![图中显示了具有 9:00 限制的 indaytotime() 函数。](https://help.qlik.com/zh-CN/cloud-services/Subsystems/Hub/Content/Resources/Images/scripting-functions-indaytotime-example1-diagram.png)

`in_day_to_time field` 字段是在前面的 LOAD 语句中使用 `indaytotime()` 函数创建的，并将日期字段、1 月 5 日 9:00 AM 的硬编码时间戳和为 0 的偏移作为函数的参数传递。1 月 5 日午夜至上午 9:00 之间发生的任何交易都将返回 TRUE。

## 示例 2 – period\_no

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

#### 加载脚本和结果

概述

加载脚本使用与第一个示例中相同的数据集和场景。

但是，在本例中，您将计算交易日期是否发生在 1 月 5 日上午 9:00 前的一天。

加载脚本

```
SET TimestampFormat='M/D/YYYY h:mm:ss[.fff] TT';

Transactions:
    Load
        *,
        indaytotime(date,'01/05/2022 9:00:00 AM', -1) as in_day_to_time
        ;
Load
*
Inline 
[
id,date,amount
8188,'01/04/2022 3:41:54 AM',25.66
8189,'01/04/2022 4:19:43 AM',87.21
8190,'01/04/2022 4:53:47 AM',53.80
8191,'01/04/2022 8:38:53 AM',69.98
8192,'01/04/2022 10:37:52 AM',57.42
8193,'01/04/2022 1:54:10 PM',45.89
8194,'01/04/2022 5:53:23 PM',82.77
8195,'01/04/2022 8:13:26 PM',36.23
8196,'01/04/2022 10:00:49 PM',76.11
8197,'01/05/2022 7:45:37 AM',82.06
8198,'01/05/2022 8:44:36 AM',17.17
8199,'01/05/2022 11:26:08 AM',40.39
8200,'01/05/2022 6:43:08 PM',37.23
8201,'01/05/2022 10:54:10 PM',88.27
8202,'01/05/2022 11:09:09 PM',95.93
];
```

结果

加载数据并打开工作表。创建新表并将这些字段添加为维度：

* `date`

* `in_day_to_time`

结果表

| 日期 | in\_day\_to\_time |
| --- | --- |
| 01/04/2022 3:41:54 AM | -1 |
| 01/04/2022 4:19:43 AM | -1 |
| 01/04/2022 04:53:47 AM | -1 |
| 01/04/2022 8:38:53 AM | -1 |
| 01/04/2022 10:37:52 AM | 0 |
| 01/04/2022 1:54:10 PM | 0 |
| 01/04/2022 5:53:23 PM | 0 |
| 01/04/2022 8:13:26 PM | 0 |
| 01/04/2022 10:00:49 PM | 0 |
| 01/05/2022 7:45:37 AM | 0 |
| 01/05/2022 8:44:36 AM | 0 |
| 01/05/2022 11:26:08 AM | 0 |
| 01/05/2022 6:43:08 PM | 0 |
| 01/05/2022 10:54:10 PM | 0 |
| 01/05/2022 11:09:09 PM | 0 |

示例 2：1 月 4 日起交易的 `indaytotime` 函数图表。

示例 2：1 月 4 日起交易的 `indaytotime` 函数图表。

示例 2：1 月 4 日起交易的 `indaytotime`indaytotime 函数图表。 ![显示 indaytotime () 函数的图表，使用为 -1 的 period_no 返回自 1 月 4 日起的事务。](https://help.qlik.com/zh-CN/cloud-services/Subsystems/Hub/Content/Resources/Images/scripting-functions-indaytotime-example2-diagram.png)

在本例中，由于 `indaytotime()` 函数中使用了 -1 的偏移作为偏移参数，因此函数确定每个交易日期是否发生在 1 月 4 日早上 9 点之前。这可以在输出表中进行验证，其中一个交易返回的布尔结果为 TRUE。

## 示例 3 – day\_start

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

#### 加载脚本和结果

概述

使用与第一个 相同的数据集和场景。

然而，在本例中，公司的政策是工作日从早上 8 点开始，到早上 8 点结束。

加载脚本

```
SET TimestampFormat='M/D/YYYY h:mm:ss[.fff] TT';

Transactions:
    Load
        *,
        indaytotime(date,'01/05/2022 9:00:00 AM', 0,8/24) as in_day_to_time
        ;
Load
*
Inline 
[
id,date,amount
8188,'01/04/2022 3:41:54 AM',25.66
8189,'01/04/2022 4:19:43 AM',87.21
8190,'01/04/2022 4:53:47 AM',53.80
8191,'01/04/2022 8:38:53 AM',69.98
8192,'01/04/2022 10:37:52 AM',57.42
8193,'01/04/2022 1:54:10 PM',45.89
8194,'01/04/2022 5:53:23 PM',82.77
8195,'01/04/2022 8:13:26 PM',36.23
8196,'01/04/2022 10:00:49 PM',76.11
8197,'01/05/2022 7:45:37 AM',82.06
8198,'01/05/2022 8:44:36 AM',17.17
8199,'01/05/2022 11:26:08 AM',40.39
8200,'01/05/2022 6:43:08 PM',37.23
8201,'01/05/2022 10:54:10 PM',88.27
8202,'01/05/2022 11:09:09 PM',95.93
];
```

结果

加载数据并打开工作表。创建新表并将这些字段添加为维度：

* `date`

* `in_day_to_time`

结果表

| 日期 | in\_day\_to\_time |
| --- | --- |
| 01/04/2022 3:41:54 AM | 0 |
| 01/04/2022 4:19:43 AM | 0 |
| 01/04/2022 04:53:47 AM | 0 |
| 01/04/2022 8:38:53 AM | 0 |
| 01/04/2022 10:37:52 AM | 0 |
| 01/04/2022 1:54:10 PM | 0 |
| 01/04/2022 5:53:23 PM | 0 |
| 01/04/2022 8:13:26 PM | 0 |
| 01/04/2022 10:00:49 PM | 0 |
| 01/05/2022 7:45:37 AM | 0 |
| 01/05/2022 8:44:36 AM | -1 |
| 01/05/2022 11:26:08 AM | 0 |
| 01/05/2022 6:43:08 PM | 0 |
| 01/05/2022 10:54:10 PM | 0 |
| 01/05/2022 11:09:09 PM | 0 |

示例 3：早上 8:00 至早上 9:00 的交易处理 `indaytotime` 函数图。

示例 3：早上 8:00 至早上 9:00 的交易处理 `indaytotime` 函数图。

示例 3：早上 8:00 至早上 9:00 的交易处理 `indaytotime`indaytotime 函数图。 ![显示 indaytotime () 函数的图表，该函数显示早上 8:00 到早上 9:00 的交易。](https://help.qlik.com/zh-CN/cloud-services/Subsystems/Hub/Content/Resources/Images/scripting-functions-indaytotime-example3-diagram.png)

因为`indaytotime()` 函数中使用了 8/24 的 `start_day` 参数，相当于早上 8:00，因此每天的开始和结束时间都是早上 8:00。因此，对于 1 月 5 日早上 8:00 到 9:00 之间发生的任何事务，`indaytotime()` 函数将返回布尔结果 TRUE。

## 示例 4–图表对象（图表）

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

#### 表达式

概述

使用与第一个 相同的数据集和场景。

然而，在本例中，未更改的数据集被加载到应用程序中。您将通过在图表对象中创建度量来计算以确定交易是否在 1 月 5 日早上 9:00 之前发生。

加载脚本

```

Transactions:
Load
*
Inline 
[
id,date,amount
8188,'01/04/2022 3:41:54 AM',25.66
8189,'01/04/2022 4:19:43 AM',87.21
8190,'01/04/2022 4:53:47 AM',53.80
8191,'01/04/2022 8:38:53 AM',69.98
8192,'01/04/2022 10:37:52 AM',57.42
8193,'01/04/2022 1:54:10 PM',45.89
8194,'01/04/2022 5:53:23 PM',82.77
8195,'01/04/2022 8:13:26 PM',36.23
8196,'01/04/2022 10:00:49 PM',76.11
8197,'01/05/2022 7:45:37 AM',82.06
8198,'01/05/2022 8:44:36 AM',17.17
8199,'01/05/2022 11:26:08 AM',40.39
8200,'01/05/2022 6:43:08 PM',37.23
8201,'01/05/2022 10:54:10 PM',88.27
8202,'01/05/2022 11:09:09 PM',95.93
];
```

结果

加载数据并打开工作表。创建新表并将该字段添加为维度：

`date`。

要确定交易是否在 1 月 5 日早上 9:00 之前发生，请创建以下度量：

`=indaytotime(date,'01/05/2022 9:00:00 AM',0)`

结果表

| 日期 | =indaytotime(date,'01/05/2022 9:00:00 AM',0) |
| --- | --- |
| 01/04/2022 3:41:54 AM | 0 |
| 01/04/2022 4:19:43 AM | 0 |
| 01/04/2022 04:53:47 AM | 0 |
| 01/04/2022 8:38:53 AM | 0 |
| 01/04/2022 10:37:52 AM | 0 |
| 01/04/2022 1:54:10 PM | 0 |
| 01/04/2022 5:53:23 PM | 0 |
| 01/04/2022 8:13:26 PM | 0 |
| 01/04/2022 10:00:49 PM | 0 |
| 01/05/2022 7:45:37 AM | -1 |
| 01/05/2022 8:44:36 AM | -1 |
| 01/05/2022 11:26:08 AM | 0 |
| 01/05/2022 6:43:08 PM | 0 |
| 01/05/2022 10:54:10 PM | 0 |
| 01/05/2022 11:09:09 PM | 0 |

`in_day_to_time` 度量是在图表对象中使用 `indaytotime()` 函数创建的，并将日期字段、1 月 5 日 9:00 AM 的硬编码时间戳和为 `0` 的偏移作为函数的参数传递。1 月 5 日午夜至上午 9:00 之间发生的任何交易都将返回 TRUE。这在结果表中得到验证。

## 示例 5 – 场景

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

#### 加载脚本和结果

概述

在本例中，包含本地影院门票销售的数据集加载到名为 Ticket\_Sales 的表中。今天是 2022 年 5 月 3 日时间是上午 11:00。

用户希望 KPI 图表对象显示迄今为止所有节目的收入。

加载脚本

```
SET TimestampFormat='M/D/YYYY h:mm:ss[.fff] TT';

Ticket_Sales:
Load
*
Inline 
[
sale ID, show time, ticket price
1,05/01/2022 09:30:00 AM,10.50
2,05/03/2022 05:30:00 PM,21.00
3,05/03/2022 09:30:00 AM,10.50
4,05/03/2022 09:30:00 AM,31.50
5,05/03/2022 09:30:00 AM,10.50
6,05/03/2022 12:00:00 PM,42.00
7,05/03/2022 12:00:00 PM,10.50
8,05/03/2022 05:30:00 PM,42.00
9,05/03/2022 08:00:00 PM,31.50
10,05/04/2022 10:30:00 AM,31.50
11,05/04/2022 12:00:00 PM,10.50
12,05/04/2022 05:30:00 PM,10.50
13,05/05/2022 05:30:00 PM,21.00
14,05/06/2022 12:00:00 PM,21.00
15,05/07/2022 09:30:00 AM,42.00
16,05/07/2022 10:30:00 AM,42.00
17,05/07/2022 10:30:00 AM,10.50
18,05/07/2022 05:30:00 PM,10.50
19,05/08/2022 05:30:00 PM,21.00
20,05/11/2022 09:30:00 AM,10.50
];
```

结果

进行以下操作：

1. 创建 KPI 对象。

2. 使用 indaytotime() 函数创建一个度量，该度量将显示到目前为止已进行的演出的所有门票销售的总和：

=sum(if(indaytotime([show time],'05/03/2022 11:00:00 AM',0),[ticket price],0))

1. 为 KPI 对象 ‘Current Revenue’ 创建标签。

2. 将度量的数字格式设置为金额。

截至 2022 年 5 月 3 日上午 11:00，门票销售总额为 52.50 美元。

indaytotime () 函数在将每次售票的放映时间与当前时间 ('05/03/2022 11:00:00 AM’) 进行比较时返回一个布尔值。对于 5 月 3 日上午 11:00 之前的任何节目，indaytotime () 函数返回布尔值 TRUE，其票价将包含在总和中。