Add Duration.fromHTMLTime()
See original GitHub issueValue 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:
- Created 4 years ago
- Reactions:1
- Comments:5 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
A couple thoughts on this:
13:30
means the time, as opposed to a period of 13 hours and 30 minutes.fromISO
method handles, though of course it returns aDateTime
, not aDuration
I’m not against providing ISO-like parsing for durations, I just don’t think it really has much to do with HTML Time.
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 correspondingtoISOTime()
, 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, theT
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. MaybetoISOTime()
should accept options for whether to omit theT
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 formhh:mm[:ss][.sss]