> For the complete documentation index, see [llms.txt](https://doc.duaer.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.duaer.com/zh/code/cookbook/luxon.md).

# 在 Duaer 里用 Luxon 处理日期

Duaer 表达式里的日期是 Luxon 的 DateTime。$now 和 $today 直接就是。Code 节点用原生 Luxon，format() 和 plus(数量, 单位) 不能照搬。
## 怎么得到一个 DateTime

$now 是现在。$today 是今天的开始。已有的字符串用 DateTime.fromISO，或用值上的 toDateTime()。

```
{{ $now.toFormat("yyyy-MM-dd") }}
{{ $json.createdAt.toDateTime().plus(7, "days").toISO() }}
```

## 和 Code 节点里的 Luxon 不一样

- 表达式里的 format() 是 Duaer 扩展。Code 节点里格式化用 toFormat()。
- 表达式里 plus 和 minus 可以写成 (数量, 单位)，例如 plus(7, "days")。Code 节点里的原生 Luxon 要写成 plus({ days: 7 })。写成两个参数不一定报错，但加不上你要的天数。
- 多句计算要用立即执行函数包起来，见[在 Duaer 里用表达式转换数据](/zh/code/expressions.md)。
## Questions

### 在 Duaer 里怎么得到今天的日期字符串？

在 Duaer 表达式里写 {{ $now.toFormat("yyyy-MM-dd") }}。$now 是 Luxon DateTime。要固定时区时先 setZone，再 toFormat。

### 为什么同一句 plus 在 Duaer Code 节点里结果不对？

Duaer 表达式接受 plus(7, "days")。Code 节点用原生 Luxon，要写成 plus({ days: 7 })。两个参数在 Code 节点里可能不报错，也不会加上这 7 天。

