代码片段
Duaer 表达式里常用的日期格式写法
Duaer 表达式里的日期是 Luxon DateTime。这里是今天、昨天、月初月末、时间戳、Excel 日期数字和北京时间的现成写法。
Duaer 里拿到当前日期
- 今天:{{ $today.toFormat("yyyy-MM-dd") }}
- 现在:{{ $now.toFormat("yyyy-MM-dd HH:mm:ss") }}
- 中文日期:{{ $now.setLocale("zh").toFormat("yyyy年M月d日 cccc") }},得到 2026年10月2日 星期五
- 昨天:{{ $today.minus({ days: 1 }).toFormat("yyyy-MM-dd") }}
- 本月第一天、最后一天:{{ $today.startOf("month").toISODate() }}、{{ $today.endOf("month").toISODate() }}
- 上周一:{{ $today.minus({ weeks: 1 }).startOf("week").toISODate() }}
把别的格式转成 Duaer 日期
- ISO 字符串:{{ DateTime.fromISO($json.createdAt).toFormat("yyyy-MM-dd") }}
- 2026/10/02 这种:{{ DateTime.fromFormat($json.date, "yyyy/MM/dd").toISODate() }}
- 秒级时间戳:{{ DateTime.fromSeconds(Number($json.ts)).toFormat("yyyy-MM-dd HH:mm") }};毫秒用 DateTime.fromMillis。
- Excel 里变成数字的日期(如 45567):{{ DateTime.fromISO("1899-12-30").plus({ days: Number($json.date) }).toISODate() }}
- 相差几天:{{ Math.floor(DateTime.fromISO($json.due).diff($today, "days").days) }}
Duaer 里的时区
$now 和 $today 按数字组织的时区算。在工作流设置里把 Timezone 改成 Asia/Shanghai,或在单个表达式里写 {{ $now.setZone("Asia/Shanghai").toFormat("HH:mm") }}。
Code 节点里也能用 DateTime 和 $now,但加减要写对象:plus({ days: 7 }),格式化用 toFormat。差别见 在 Duaer 里用 Luxon 处理日期。
常见问题
Duaer 里的 $now 为什么比北京时间差 8 小时?
Duaer 数字组织的时区还是 UTC。在工作流设置里把 Timezone 改成 Asia/Shanghai,或在表达式里用 setZone("Asia/Shanghai")。
Duaer 读 Excel 时日期变成了 45567 这样的数字怎么办?
那是 Excel 的日期序号。在 Duaer 表达式里用 DateTime.fromISO("1899-12-30").plus({ days: 数字 }) 转回日期。
同组
日期
在 Duaer 里用 Luxon 处理日期
Duaer 表达式里的日期是 Luxon 的 DateTime。$now 和 $today 直接就是。Code 节点用原生 Luxon,format() 和 plus(数量, 单位) 不能照搬。
转换Duaer 日期上的转换方法
Duaer 表达式里,$now 和 $today 是 Luxon 的 DateTime。字符串或数字要先 toDateTime(),再调用 format、plus、setZone。
代码片段在 Duaer 里分组求和、计数(类似数据透视表)
在 Duaer 里用 Summarize 节点按地区、客户或分类分组,算合计、个数、平均值,不用写代码。