question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add Duration.fromHTMLTime()

See original GitHub issue

Value of HTML’s <input type="time"> have below patterns:

  • hh:mm
  • hh:mm:ss
  • hh:mm:ss.S
  • hh:mm:ss.SS
  • hh:mm:ss.SSS

These are expressed as below regular expression:

/^(\d{2}):(\d{2})(:(\d{2}))?(\.(\d{1,3}))?$/

cf. https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#times

My idea

Not tested

Implemention

function fromHTMLTime (text) {
  const matched = text.match(/^(\d{2}):(\d{2})(:(\d{2}))?(\.(\d{1,3}))?$/)

  if (!matched) {
    return Duration.invalid('unparsable', `the input "${text}" can't be parsed as HTML time`)
  }

  const getValue = index => matched[index] ? Number(matched[index]) : 0
  const hours = getValue(1)
  const minutes = getValue(2)
  const seconds = getValue(4)
  const milliseconds = getValue(6)
  return Duration.fromObject({
    hours,
    minutes,
    seconds,
    milliseconds
  })
}

Usage example

<input type="time" id="time" value="13:30">
const timeElement = document.getElementById('time')
const value = timeElement.value  // "13:30"
const duration = Duration.fromHTMLTime(value)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
icambroncommented, Dec 15, 2019

A couple thoughts on this:

  • It’s not clear to me that HTML time is meant to specify a duration, as opposed to a specific time on some (externally-provided) date, i.e. it seems like 13:30 means the time, as opposed to a period of 13 hours and 30 minutes.
  • As a time, it’s just an ISO time and the fromISO method handles, though of course it returns a DateTime, not a Duration

I’m not against providing ISO-like parsing for durations, I just don’t think it really has much to do with HTML Time.

0reactions
thomas-darlingcommented, Oct 6, 2020

Hmm, on a second thought, I actually agree with @icambron regarding the name.

The best name for this would be fromISOTime(...) - and then I’d suggest we also add a corresponding toISOTime(), so we can do a full roundtrip in this format.

This does raise a question though: While all ISO times can be represented as durations, not all durations can be represented as ISO times, as that format is limited to 24 hours. I think the solution here is to simply return null if toISOTime() is called on a duration that is longer than 24 hours. This would be consistent with the current behavior, when such methods are called on an invalid duration.

Also note that according to https://en.wikipedia.org/wiki/ISO_8601#Times, the time format has changed slightly in the latest version of the spec. Basically, it may now start with a T - and if the value could be confused with a date, the T is required.

So, the format is essentially Thh[mm][ss][.sss] in the short form and [T]hh[:mm][:ss][.sss] in the extended form. Maybe toISOTime() should accept options for whether to omit the T when possible, and whether to use the short or extended form - but on the other hand, the pragmatic choice might be to just always use the form hh:mm[:ss][.sss]

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get AM/PM from HTML time input - javascript
From there you can get the hours using .getHours() (0-23) and minutes using .getMinutes() (0-59). See this for more about JavaScript Dates.
Read more >
How to add date and time picker using only one tag in HTML
We can add date and time picker in only one tag without using the two different tags. Let's discuss it one by one....
Read more >
Date and time formats used in HTML - MDN Web Docs
Adding a day to the year every four years essentially makes the average year 365.25 days long, which is close to correct.
Read more >
Display Current Date and Time Using HTML and ...
Date object is created with the new Date() constructor. The current date and time is stored inside javascript variable. Then using innerHTML property,...
Read more >
Add Month(s) to a Date in JavaScript or Node.js - Future Studio
This tutorial shows you how to add one or more months to a date in JavaScript. Node.js Series Overview. Node.js; Strings; Streams; Date...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found