dayjs 传入 string 和 number 的执行区别

开发中发现一个时间解析错误的 bug。dayjs('1702438724297').format('YYYY-MM-DD') 解析出来预期是 2023-12-13,结果输出 1705-09-26。
这是因为将时间戳作为字符串传递时,dayjs 会尝试解析它作为一个 ISO 8601 日期字符串,而不是将其视为 Unix 时间戳(毫秒)。在这种情况下,dayjs 无法正确解析字符串,因此返回的日期可能是无效的。
当你将时间戳作为数字传递时,dayjs 会将其视为 Unix 时间戳(毫秒),并正确解析和格式化日期。
版权声明:
作者:东明兄
链接:https://blog.crazyming.com/note/3204/
来源:CrazyMing
文章版权归作者所有,未经允许请勿转载。


共有 0 条评论