# Date & Time

> 当前时刻与今天，均为 Luxon DateTime 值。

Source: https://docs.sommatic.ai/zh/expressions/reference/date-time/

每个示例都在已部署的表达式引擎上、基于页面末尾的[示例上下文](#example-context)运行过。

## `$now`

变量 · `DateTime` · 来源: `Luxon · DateTime`

```javascript
$now
```

一个 [Luxon](/zh/expressions/luxon/) `DateTime`，在节点开始解析其配置时取值：同一节点的所有表达式看到的是同一时刻。它采用服务器时区，因此在格式化或比较日历日期前，请先设置所需时区。

在纯字段中，`$now` 保留其类型，节点收到的是 `DateTime`；在混合字段中，它以带引号的 JSON 文本插入。只要结果要写入文本，就请自行格式化（`toISO()`、`toFormat(…)`）。

### 成员

| 签名 | 类型 | 说明 |
| --- | --- | --- |
| `$now.toISO()` | `string` | 带时区偏移的 ISO 8601 日期时间。 |
| `$now.toISODate()` | `string` | 仅日期：`yyyy-MM-dd`。 |
| `$now.toFormat(format)` | `string` | 使用 Luxon 标记的文本：`yyyy` 年、`LL` 月、`dd` 日、`HH` 时、`mm` 分、`ss` 秒。 |
| `$now.toMillis()` | `number` | 自 1970-01-01 UTC 起的毫秒数。 |
| `$now.toSeconds()` | `number` | 自 1970-01-01 UTC 起的秒数，含小数。 |
| `$now.setZone(zone)` | `DateTime` | 同一时刻在另一个 IANA 时区中的表示，例如 `America/Bogota` 或 `UTC`。 |
| `$now.plus({ days, hours, minutes… })` | `DateTime` | 之后的某个时刻。 |
| `$now.minus({ days, hours, minutes… })` | `DateTime` | 之前的某个时刻。 |
| `$now.startOf(unit)` | `DateTime` | `year`、`month`、`week`、`day`、`hour` 或 `minute` 的起始时刻。 |
| `$now.endOf(unit)` | `DateTime` | 该单位的最后一毫秒。 |
| `$now.diff(other, unit)` | `Duration` | 两个日期之间的时长；用 `.as('days')` 或 `.toObject()` 读取。 |
| `$now.weekday` | `number` | 星期几，1（星期一）到 7（星期日）。另有 `year`、`month`、`day`、`hour`、`minute`。 |
| `$now.constructor` | `DateTime class` | `DateTime` 类本身，用于解析日期：`$now.constructor.fromISO(text)`、`.fromFormat(text, format)`、`.fromMillis(ms)`。 |

### 示例

```javascript
{{ $now.toISO() }}
```

结果 (string):

```json
"2026-11-26T14:30:00.000Z"
```

```javascript
{{ $now.setZone('America/Bogota').toFormat('yyyy-LL-dd HH:mm') }}
```

结果 (string):

```json
"2026-11-26 09:30"
```

```javascript
{{ $now.plus({ days: 3 }).toISODate() }}
```

结果 (string):

```json
"2026-11-29"
```

```javascript
{{ $now.toMillis() }}
```

结果 (number):

```json
1795703400000
```

距某个以 ISO 文本传入的日期已过去的小时数。

```javascript
{{ $now.diff($now.constructor.fromISO($json.body.created_at), 'hours').as('hours') }}
```

结果 (number):

```json
29.25
```

波哥大现在是周末吗？

```javascript
{{ $now.setZone('America/Bogota').weekday >= 6 }}
```

结果 (boolean):

```json
false
```

混合字段中未格式化的 $now 会以带引号的 JSON 插入。

```javascript
Sent at {{ $now }}
```

结果 (string):

```json
"Sent at \"2026-11-26T14:30:00.000Z\""
```

### 说明

- DateTime 的全部方法、getter 和静态构造函数见 [Luxon 参考](/zh/expressions/luxon/)。
- `DateTime` 不是全局名称：请通过 `$now.constructor` 获取该类。

在 JSONata 中: 不可用：$$now 以对象形式传入，JSONata 无法调用其方法。请使用 JSONata 函数 $now() 和 $fromMillis()。

## `$today`

变量 · `DateTime` · 来源: `Luxon · DateTime`

```javascript
$today
```

服务器时区中当天的午夜：`$now.startOf('day')`。要获取另一时区中当天的起始时刻，请使用 `$now.setZone(zone).startOf('day')`；转换 `$today` 得到的是服务器当天的午夜在该时区下的表示。

### 示例

```javascript
{{ $today.toISODate() }}
```

结果 (string):

```json
"2026-11-26"
```

从今天起 30 天后的到期日。

```javascript
{{ $today.plus({ days: 30 }).toISODate() }}
```

结果 (string):

```json
"2026-12-26"
```

另一时区中今天的起始时刻。

```javascript
{{ $now.setZone('America/Bogota').startOf('day').toISO() }}
```

结果 (string):

```json
"2026-11-26T00:00:00.000-05:00"
```

在 JSONata 中: 无法在 JSONata 中使用，原因与 $now 相同。

## 示例上下文

示例基于以下数据运行：当前节点的输入（`$json`）、已运行的节点（`$(…)`）、本次运行的 ID 以及当前节点。时钟固定为 `2026-11-26T14:30:00.000Z`，服务器时区为 UTC，因此各页面中 `$now` 与 `$today` 的值都相同。

```json
{
  "$json": {
    "body": {
      "order_id": "ord_1042",
      "email": "ana.perez@example.com",
      "customer": {
        "name": "Ana Pérez",
        "segment": "gold"
      },
      "priority": "",
      "items": [
        {
          "sku": "A-100",
          "qty": 2,
          "price": 10.5
        },
        {
          "sku": "B-200",
          "qty": 0,
          "price": 4
        },
        {
          "sku": "C-300",
          "qty": 1,
          "price": 25
        }
      ],
      "tags": [
        "new",
        "web",
        "new"
      ],
      "created_at": "2026-11-25T09:15:00Z"
    },
    "headers": {
      "content-type": "application/json"
    },
    "query": {},
    "method": "POST"
  },
  "nodes": {
    "Webhook Receiver": "= $json",
    "Normalize input": {
      "correlation_id": "c-7f3a",
      "channel": "web"
    },
    "Classify": {
      "label": "billing",
      "confidence": 0.92
    }
  },
  "run": {
    "$flow_instance_result_id": "fir-xxxx",
    "$flow_instance_id": "fi-xxxx",
    "$flow_definition_id": "fd-xxxx",
    "$flow_version_id": "fv-xxxx",
    "$flow_slug": "order-intake"
  },
  "node": {
    "node_id": "node-notify",
    "label": "Notify customer"
  }
}
```

串联工作流的示例基于第二个上下文运行：一个由 `workflow.chain` 启动的工作流，其 `workflow.trigger` 节点的标签为 `Chain Entry Point`。

```json
{
  "$json": {
    "order_id": "ord_1042",
    "customer_email": "ana.perez@example.com",
    "amount": 46,
    "chain_context": {
      "caller_flow_definition_id": "fd-caller-xxxx",
      "caller_flow_instance_id": "fi-caller-xxxx",
      "caller_flow_slug": "order-intake",
      "caller_node_id": "node-chain-billing",
      "correlation_id": "chain-7Hq2Lw9sKd3pXa1mZr4t",
      "root_flow_instance_result_id": "fir-root-xxxx"
    }
  },
  "run": {
    "$flow_instance_result_id": "fir-hop-xxxx",
    "$flow_instance_id": "fi-hop-xxxx",
    "$flow_definition_id": "fd-billing-xxxx",
    "$flow_version_id": "fv-billing-xxxx",
    "$flow_slug": "billing-review",
    "$input": {
      "data": {
        "order_id": "ord_1042",
        "customer_email": "ana.perez@example.com",
        "amount": 46
      },
      "chain_context": {
        "caller_flow_definition_id": "fd-caller-xxxx",
        "caller_flow_instance_id": "fi-caller-xxxx",
        "caller_flow_slug": "order-intake",
        "caller_node_id": "node-chain-billing",
        "correlation_id": "chain-7Hq2Lw9sKd3pXa1mZr4t",
        "root_flow_instance_result_id": "fir-root-xxxx"
      }
    }
  },
  "node": {
    "node_id": "node-create-task",
    "label": "Create review task"
  }
}
```

示例运行所基于的编排器提交 31e874b.
