簡體   English   中英

Day.js 支持 JSON 格式的 Timespan 解析(如 14.06:10:36)

[英]Day.js support for JSON formatted Timespan parsing (e.g. 14.06:10:36)

到目前為止,我一直在使用 Moment.js 來解析來自 REST API 的 JSON 格式的時間跨度。JSON 響應中的時間跨度采用非常常見的“dd.hh:mm:ss”格式(例如“14.06:10:36”為 14 天 6 小時 10 分 36 秒)格式。

Moment.js 有一種簡單的解析方法,但令我驚訝的是我找不到用 day.js 解析這些時間跨度字符串的方法。 在大多數情況下,day.js 是 moment.js 的一個很好的替代品,它現在處於維護模式,並且占用空間更大 memory。

有什么我錯過的嗎,或者有沒有辦法用 day.js 解析時間跨度字符串?

有一個持續時間插件 那可能就是你要找的。

您正在尋找Dayjs durations 您可能想先使用正則表達式來解析它。

import dayjs from "dayjs";
import duration from "dayjs/plugin/duration";

dayjs.extend(duration);

const str = "14.06:10:36";

const groups = str.match(/(\d{2})\.(\d{2}):(\d{2}):(\d{2})/);


const dayjsDuration = dayjs.duration({
  days: parseInt(groups[1]!),
  hours: parseInt(groups[2]!),
  minutes: parseInt(groups[3]!),
  seconds: parseInt(groups[4]!)
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM