react+antd使用dayjs将时间戳对象转为可读格式“YYYY-MM-DD”

antd有时候获取的时间对象为{ "$L": "en", "$d": "2023-09-01T10:06:48.590Z", "$x": {}, "$y": 2023, "$M": 8, "$D": 1, "$W": 5, "$H": 18, "$m": 6, "$s": 48, "$ms": 590 }这样的时间戳对象,

//包含时间信息的 JSON 对象
const timeObject = {
  "$L": "en",
  "$d": "2023-09-01T10:06:48.590Z",
  "$x": {},
  "$y": 2023,
  "$M": 8,
  "$D": 1,
  "$W": 5,
  "$H": 18,
  "$m": 6,
  "$s": 48,
  "$ms": 590
};

// 使用 dayjs 解析时间字符串并格式化为 'YYYY-MM-DD' 格式
const formattedDate = dayjs(timeObject["$d"]).format('YYYY-MM-DD');

console.log(formattedDate); // 输出:'2023-09-01'

这样就能够将时间戳对象转为可读格式“YYYY-MM-DD”了

猜你喜欢

转载自blog.csdn.net/niconicon____/article/details/133378034