dayjs
dayjs copied to clipboard
feat: add plugin/duration ASP.NET style TimeSpan
Adding a duration creation with the ASP.NET TimeSpan format
Request : #2179 Include src, types, test 100% test coverage :D
Perfomances
🟢: - 1s 🟠: + 1s 🔴: + 1.5s
- Not count first run, each run 5 times* I didn't count the first execution, each line was executed 5 times to make an average
fn | time |
---|---|
duration('23:59') | 🟠1.04s |
duration('23:59:59') | 🟢0.98s |
duration('23:59:59.999') | 🟠1.11s |
duration('7.23:59:59.999') | 🟠1.05s |
duration('7 23:59:59.999') | 🟠1.08s |
Any updates on this?
Conflict free: https://github.com/iamkun/dayjs/compare/dev...moander:dayjs:conflict-fix
I'm using the following regex as a quickfix
function parseDuration(str) {
if (typeof str === 'string' && !str.startsWith('P')) {
const regex = /^((?<days>\d+)[. ])?(?<hh>\d\d):(?<mm>\d\d)(:(?<ss>\d\d)(\.(?<ms>\d{1,9}))?)?$/
const match = str.match(regex)?.groups
if (match) {
return dayjs.duration(`P${match.days || 0}DT${match.hh || 0}H${match.mm || 0}M${match.ss || 0}.${match.ms || 0}S`)
}
}
return dayjs.duration(str)
}
console.log(parseDuration('23:59')?.toISOString())
console.log(parseDuration('23:59:59')?.toISOString())
console.log(parseDuration('23:59:59.999')?.toISOString())
console.log(parseDuration('7.23:59:59.999')?.toISOString())
console.log(parseDuration('7 23:59:59.999')?.toISOString())
Output:
PT23H59M
PT23H59M59S
PT23H59M59.999S
P7DT23H59M59.999S
P7DT23H59M59.999S
Do you have any update to share on when this PR will be merged ?