constructor(cronTime: string | Date | DateTime, xxx)
有函数接口如上传参:可传入Date,DateTime,string
但在接口传入的时候都是会被转为字符串,比如swagger中设置了三种类型的参数分别为:
// 转为 Date类型 2023-07-05T04:01:11.000Z
example: new Date('2023/7/5 12:01:11'),
// 转为 DateTime类型 2023/7/5 12:01:11
example: format(new Date(addDays(new Date(), 1)), 'yyyy/MM/dd hh:ii:ss'), // 1d后 DateTime 格式
// 字符串
example: '12 * * * * *', // 定时模式格式
怎么判断是什么类型呢?
使用luxon
的fromXX
和isValid
time = DateTime.fromISO(time as string).isValid ||
DateTime.fromSQL(time as string).isValid ||
DateTime.fromFormat(time as string, 'yyyy/MM/dd HH:mm:ss').isValid
? new Date(time as string)
: time;
// ISO格式:
2016
2016-05
201605
2016-05-25
20160525
2016-05-25T09
2016-05-25T09:24
2016-05-25T09:24:15
2016-05-25T09:24:15.123
2016-05-25T0924
2016-05-25T092415
2016-05-25T092415.123
2016-05-25T09:24:15,123
2016-W21-3
2016W213
2016-W21-3T09:24:15.123
2016W213T09:24:15.123
2016-200
2016200
2016-200T09:24:15.123
09:24
09:24:15
09:24:15.123
09:24:15,123
// SQL格式:
DateTime.fromSQL("2017-05-15");
DateTime.fromSQL("2017-05-15 09:24:15");
DateTime.fromSQL("09:24:15");
// fromFormat: 自定义匹配的格式