dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

feat: add plugin/duration ASP.NET style TimeSpan

Open KronosDev-Pro opened this issue 2 years ago • 4 comments

Adding a duration creation with the ASP.NET TimeSpan format

Request : #2179 Include src, types, test 100% test coverage :D

image

KronosDev-Pro avatar Jan 10 '23 22:01 KronosDev-Pro

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

KronosDev-Pro avatar Jan 10 '23 22:01 KronosDev-Pro

Any updates on this?

Conflict free: https://github.com/iamkun/dayjs/compare/dev...moander:dayjs:conflict-fix

moander avatar Feb 16 '24 16:02 moander

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

moander avatar Feb 16 '24 17:02 moander

Do you have any update to share on when this PR will be merged ?

YohanSciubukgian avatar Mar 26 '24 03:03 YohanSciubukgian