本页内容
Sommatic 文档
打开控制台

Date & Time

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

以 Markdown 查看
在以下应用中打开

每个示例都在已部署的表达式引擎上、基于页面末尾的示例上下文运行过。

$now

$now

一个 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)。

示例

{{ $now.toISO() }}
结果 string
"2026-11-26T14:30:00.000Z"
{{ $now.setZone('America/Bogota').toFormat('yyyy-LL-dd HH:mm') }}
结果 string
"2026-11-26 09:30"
{{ $now.plus({ days: 3 }).toISODate() }}
结果 string
"2026-11-29"
{{ $now.toMillis() }}
结果 number
1795703400000

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

{{ $now.diff($now.constructor.fromISO($json.body.created_at), 'hours').as('hours') }}
结果 number
29.25

波哥大现在是周末吗?

{{ $now.setZone('America/Bogota').weekday >= 6 }}
结果 boolean
false

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

Sent at {{ $now }}
结果 string
"Sent at \"2026-11-26T14:30:00.000Z\""

说明

  • DateTime 的全部方法、getter 和静态构造函数见 Luxon 参考。
  • DateTime 不是全局名称:请通过 $now.constructor 获取该类。

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

$today

$today

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

示例

{{ $today.toISODate() }}
结果 string
"2026-11-26"

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

{{ $today.plus({ days: 30 }).toISODate() }}
结果 string
"2026-12-26"

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

{{ $now.setZone('America/Bogota').startOf('day').toISO() }}
结果 string
"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"
  }
}
已运行的节点(标签 → 输出) JSON
{
  "Webhook Receiver": "= $json",
  "Normalize input": {
    "correlation_id": "c-7f3a",
    "channel": "web"
  },
  "Classify": {
    "label": "billing",
    "confidence": 0.92
  }
}
运行与当前节点 JSON
{
  "$execution": {
    "id": "fir-xxxx",
    "flow_instance_id": "fi-xxxx",
    "flow_definition_id": "fd-xxxx",
    "flow_version_id": "fv-xxxx",
    "flow_slug": "order-intake"
  },
  "$node": {
    "id": "node-notify",
    "label": "Notify customer"
  }
}

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

安装智能体工具

为你的编程智能体接入 Sommatic AI 官方文档:技能包、文档 MCP 服务器与 Markdown 页面,无需再抓取网页。

安装 Sommatic AI CLI,并为检测到的智能体配置合适的插件。

npx @sommatic/cli agent setup
智能体如何读取本文档