Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML time tag for duration

I need to display the duration of a video. Should I use <time> or should it be used only for displaying time as in a clock?

Example

Is this correct HTML5?

<p>Duration: <time>3:30 min</time>.</p>

Or should <time> be used only on a situation like this?

<p>Good morning. The actual time is: <time>9:45</time></p>.

Docs

MDN presents the following definition:

The HTML element represents either a time on a 24-hour clock or a precise date in the Gregorian calendar (with optional time and timezone information). This element is intended to be used presenting dates and times in a machine readable format. This can be helpful for user agents to offer any event scheduling for user's calendar.

However, the definition in W3C is somewhat different, and it refers duration:

Specifies the date or time that the element represents.

Value: Any one of the following:

  • (...)

  • a valid duration string as defined in the [HTML5] specification

Examples:

PT4H18M3S

4h 18m 3s

So, I'm not sure if I should be using <time> in this situation or not. Any thoughts?

like image 273
Cthulhu Avatar asked Jul 22 '15 09:07

Cthulhu


People also ask

What is the tag for time in HTML?

<time>: The (Date) Time element. The <time> HTML element represents a specific period in time. It may include the datetime attribute to translate dates into machine-readable format, allowing for better search engine results or custom features such as reminders.

Is there a tag for date in HTML?

The <input type="date"> defines a date picker. The resulting value includes the year, month, and day. Tip: Always add the <label> tag for best accessibility practices!

How do I show date and time in HTML?

The <time> datetime Attribute in HTML is used to defines the machine-readable date/time of the <time> element. The date-time is inserted in the format YYYY-MM-DDThh:mm:ssTZD.


2 Answers

Yes, you can use the time element for durations.

But the value has to be a valid duration string, which "3:30 min" is not.

You can use the datetime attribute to specify the correct value, and keep "3:30 min" as element content, e.g.:

<time datetime="3m 30s">3:30 min</time>
like image 69
unor Avatar answered Oct 13 '22 18:10

unor


If the spec allows the time element to have a duration as a value, it means it can be used this way. The only limitation I found in the spec is that you can't specify months or years for a duration — only weeks, days, hours, minutes, and seconds. But it shouldn't be a problem for a duration of the video).

like image 23
Ilya Streltsyn Avatar answered Oct 13 '22 19:10

Ilya Streltsyn