Date 值方法
Studio SDK 返回的 Date 字段按字符串处理,格式为 YYYY-MM-DD。
赋值
const dueDate = String(payload.input.dueDate || '').trim();
await context.form.update({
recordId: 'record-id',
data: {
due_date: dueDate,
},
});
可接受入参示例:
| 入参 | 归一化结果 |
|---|---|
'2026-06-18' | '2026-06-18' |
new Date('2026-06-18') | 写入前格式化 |
null | 字段允许清空时用于清空 |
常量值
日期相关字段按字符串写入。时间字段使用 HH:mm:ss;日期、周、月、季度、年字段使用 YYYY-MM-DD。
对于周期字段,常量值定义为代表该周期的日期。建议统一使用周期开始日期,脚本里更可预测。
| 字段类型 | 常量值 | 含义 |
|---|---|---|
| 日期 | '2026-06-18' | 2026 年 6 月 18 日 |
| 时间 | '09:30:00' | 09:30 |
| 周 | '2026-06-14' | 从 2026 年 6 月 14 日开始的周 |
| 月 | '2026-06-01' | 2026 年 6 月 |
| 季度 | '2026-04-01' | 2026 年第 2 季度 |
| 年 | '2026-01-01' | 2026 年 |
const defaultValues = {
date_value: '2026-06-18',
time_value: '09:30:00',
week_value: '2026-06-14',
month_value: '2026-06-01',
quarter_value: '2026-04-01',
year_value: '2026-01-01',
};
await context.form.update({
recordId: 'record-id',
data: defaultValues,
});
不要把展示标签作为存储值写入。
const invalidPeriodValues = {
week_value: '2026-W25',
month_value: '2026-06',
quarter_value: '2026-Q2',
year_value: '2026',
};
区间和多选字段使用相同的单项格式。
const periodRange = ['2026-04-01', '2026-07-01'];
const selectedMonths = ['2026-06-01', '2026-07-01'];
转时间戳
const dateText = String(record.data.due_date || '');
const timestamp = new Date(`${dateText}T00:00:00`).getTime();
if (Number.isNaN(timestamp)) {
throw new Error('DUE_DATE_INVALID');
}
日期操作
function formatDate(value: Date) {
const year = value.getFullYear();
const month = String(value.getMonth() + 1).padStart(2, '0');
const day = String(value.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}
const today = formatDate(new Date());
const tomorrowDate = new Date(`${today}T00:00:00`);
tomorrowDate.setDate(tomorrowDate.getDate() + 1);
const tomorrow = formatDate(tomorrowDate);
只有两个值都使用 YYYY-MM-DD 格式时,才用字符串直接比较。
const overdue = String(record.data.due_date || '') < today;