HTML5 Doctor » microformats http://html5doctor.com helping you implement HTML5 today Wed, 02 Mar 2016 08:08:54 +0000 en-US hourly 1 Extending HTML5 — Microformats https://stevefaulkner.github.io/html5doctored/microformats/ https://stevefaulkner.github.io/html5doctored/microformats/#comments Tue, 17 Aug 2010 13:30:18 +0000 http://html5doctor.com/?p=2304 HTML5 contains a bunch of new semantic goodness, but sometimes we need more semantics than what’s available. This is the first article in a series looking at various ways to extend HTML5 — first up, microformats.

]]>
While HTML5 has a bunch of semantic elements, including new ones like <article> and <nav>, sometimes there just isn’t an element with the right meaning. What we want are ways to extend what we’ve got, to add extra semantics that are machine-readable — data that a browser, script, or robot can use.

Native ways to extend HTML

There were five fundamental ways we could extend HTML 4:

  • <meta> element
  • class attribute
  • rel attribute
  • rev attribute
  • profile attribute

In HTML5, rev has fallen by the wayside, becoming obsolete since hardly anyone used it correctly, and because it can be replaced by rel. profile is also obsolete, and there is no support for namespaces in HTML5. However, <meta>, class, and rel are all in HTML5. In fact, <meta> now has spec-defined names and a way to submit new name values, and rel has several new link types defined in the HTML5 specification and a way to submit more too.

Even better, WAI-ARIA’s role and aria-* attributes are allowed in HTML5, and HTML5 validators can check HTML5+ARIA. Other new methods of extending HTML5 include custom data attributes (data-*), microdata, and RDFa.

Finally there are microformats. As Dr. Bruce touched on microformats in his article on the <time> element, let’s delve a little deeper into what microformats are and how to use them in HTML5.

Introducing microformats

Microformats are a collection of vocabularies for extending HTML with additional machine-readable semantics. They are designed for humans first and machines second. This is currently accomplished via agreed-upon class, rel, rev and profile attributes, coding patterns, and nesting.

Being machine readable means a robot or script that understands the microformat vocabulary being used can understand and process the marked-up data. Each microformat defines a specific type of data and is usually based on existing data formats — like vcard (address book data; RFC2426) and icalendar (calendar data; RFC 2445) — or common coding patterns (‘paving the cowpaths’ of the web).

Despite their humble beginnings, microformats have also been a runaway success, and they're far more widely deployed than other “big S” Semantic web technologies. For example, many services, such as Twitter and Flickr, offer profile information in hCard format by default, so you may already have a microformatted profile even if you’ve never used microformats before.

Microformat specifications

There are currently 34 microformats specifications (listed below) in varying levels of completion, and you can find out more about them on the microformats wiki.

Published microformats

  • Elemental (simple) microformats
    • XFN — specify relationships with people
    • XMDP — add metadata profiles
    • VoteLinks — indicate agreement or disagreement with, or indifference to, the link’s destination
    • rel-nofollow — don’t give ‘weight’ to a link (don’t give ‘Google juice’; mainly for search engines)
    • rel-license — specify license information
    • rel-tag — add tags
  • Compound microformats
    • hCard — contact information for people and organisations
    • hCalendar — time-based information, such as events
    • XOXO — outlines

Draft microformats

  • adr
  • geo
  • hAtom
  • hAudio
  • hListing
  • hMedia
  • hNews
  • hProduct
  • hRecipe
  • hResume
  • hReview
  • rel-directory
  • rel-enclosure
  • rel-home
  • rel-payment
  • robots exclusion
  • Species
  • xFolk

Despite being drafts, many of these are in widespread use.

Microformat patterns

These common coding patterns are just best practices that are frequently used in writing plain old semantic HTML (POSH) to create microformats.

  • <abbr> design pattern
  • class design pattern
  • date design pattern
  • datetime design pattern
  • include pattern
  • value class pattern
  • rel design pattern

These specifications and patterns cover many common types of data. They’ve been created by a grass-roots organisation of interested people, and anyone is welcome to contribute or even propose a new microformat.

A lightning introduction to using microformats

For those of you that haven't used microformats before, I’ll briefly introduce some simple microformats — hopefully so simple you’ll be encouraged to use them right away!

Using rel-license for licensing information

Adding license information is quite a common activity, and while we can add a link to Creative Commons or another license easily enough, someone would have to read it to understand the content’s license:

<small>This article is licensed under a 
  <a href="http://creativecommons.org/licenses/by-nc-sa/2.0/">
  Creative Commons Attribution Non-commercial Share-alike 
  (By-<abbr>NC</abbr>-<abbr>SA</abbr>) license</a>.
</small>

If this information was machine-readable, search engines could use it to help consumers searching by license. By using the rel-license microformat to add rel="license" to the license link (indicating it’s the license for the page’s main content), we can do just that:

<small>This article is licensed under a 
  <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/2.0/">
  Creative Commons Attribution Non-commercial Share-alike 
  (By-<abbr>NC</abbr>-<abbr>SA</abbr>) license</a>.
</small>

This article is licensed under a Creative Commons Attribution Non-commercial Share-alike (By-NC-SA) license.

A rel-license-formatted code sample with an example rendering

This may be so easy you don’t even realise you’ve just microformatted the link! In fact, Google already uses this data to allow us to search by license (look in , in the extra options).

Google advanced search options, showing the “Usage rights” pulldown

Using XHTML Friends Network (XFN)

Maybe we should term this one eXtensible Friends Network instead :-) XFN is a way of specifying your relationship with people — everything from “met” to “sweetheart” — using the rel attribute on a link to their homepage.

There are two main values: rel="contact" for someone you know how to get in touch with, and rel="me". The rel="me" value allows you to claim ownership of your various websites, including your accounts on social networks. For example, I could have a profile with a link to my Twitter account:

<p>Oli Studholme — <a href="http://twitter.com/boblet">follow me on
  Twitter (@boblet)</a></p>

A person can infer that @boblet is my Twitter username, but by adding rel="me" we can state this relationship in a machine-readable way.

<p>Oli Studholme — <a rel="me" href="http://twitter.com/boblet">follow me on
  Twitter (@boblet)</a></p>

In order to actually work, this would need to be on my personal homepage, with the same homepage added to my Twitter profile. By doing this, a social web app that understands XFN could confirm @boblet is me, check my friends on Twitter, check if those people are already registered, and then allow me to follow them all with one click — much easier.

For more on this idea, check out Identity consolidation and the XFN rel="me" value, the RelMeAuth proposal on the microformats wiki, and Google’s Social Graph API. It seems Facebook is supporting rel="me" too.

Rel-license and XFN are simple rel-based microformats, but even with their simplicity you can see the potential power in this machine-readable stuff. Now let’s look at microformats for contact and event information.

Using hCard for contact information

Almost every website has an about page with some contact information:

<p>Oli Studholme — <a href="http://oli.jp/">http://oli.jp</a>, or 
<a href="http://twitter.com/boblet">follow me on Twitter (@boblet)</a>.</p>

Unfortunately, adding someone’s contact information to your phone or address book generally involves a lot of copying and pasting. If the data was machine-readable, however, we could use a tool to do that. Let’s add the hCard microformat to this code snippet.

<p class="vcard"><span class="fn">Oli Studholme</span> — 
<a class="url" href="http://oli.jp/">http://oli.jp</a>, or 
<a class="url" href="http://twitter.com/boblet">follow me on Twitter 
(@<span class="nickname">boblet</span>)</a>.</p>

Oli Studholme — http://oli.jp, or follow me on Twitter (@boblet).

An hCard-formatted code sample with an example rendering

So we’ve added a bunch of classes. There’s nothing special about that, of course, until you realise they're all part of the hCard microformat. The first one is vcard on the containing <p> element, indicating that there's hCard data here. Then we have fn, which stands for full name, url for an associated homepage, and nickname for, well, a nickname.

This is a simple example, and in fact a valid hCard (generally) only requires vcard and fn. But hCard has much more depth. We can mark up all kinds of contact-related data: addresses, company information, even a profile photo. For example, we can explicitly specify a given name and family name (this is required for Chinese, Korean, Japanese and Vietnamese names), even middle names and titles.

<span class="vcard"><span class="fn n" lang="ja"><span class="family-name">
スタッドホルム</span>・<span class="given-name">オリ</span></span></span>

The additional n value is actually required, but can be inferred (the so-called “implied n optimisation”) and therefore omitted for names that follow these patterns:

  • given-name family-name
  • family-name, given-name

So what’s the benefit of this? Well, there are several tools that will convert this hCard-microformatted data into a vcard file we can download and automatically add to our address book. Nifty!

The Firefox Operator plugin’s hCard export options
The Firefox plugin Operator can save hCard-formatted content as a vcard, or add it to Yahoo Contacts

It’s also recognised by Google’s Rich Snippets and Yahoo’s SearchMonkey, useful for the contact information on any company website.

A preview of how rich snippet data may be incorporated into Google’s search results—in this example “Osaka” and “designer” Microformat hCard data extracted by the Google Rich Snippet testing tool
Google’s Rich Snippet testing tool for an hCard (Note the “Osaka - designer” in the search preview)

Organisations, addresses, and phone numbers

Smaller company websites often have the company name and contact details in the footer, so let’s briefly see how to do that.

<p class="vcard">
<a class="fn org url" href="http://www.w3.org/Consortium/contact-keio" title="Contact Information for W3C/Keio"><span class="organization-name">W3C</span>/<span class="organization-unit">Keio</span></a><br>
<span class="adr"><a class="extended-address" href="http://www.keio.ac.jp/english/about_keio/campus_info/sfc1.html">Keio University Shonan Fujisawa Campus</a><br>
<span class="street-address">5322 Endo</span>, <span class="locality">Fujisawa city</span>,<br>
<span class="region">Kanagawa prefecture</span> <span class="postal-code">252-8520</span> <b class="country">Japan</b><br>
<span class="tel">Telephone: <span class="value">+81-466-49-1170</span></span><br>
<span class="tel"><span class="type">Fax</span>: <span class="value">+81-466-49-1171</span></span></span>
</p>

W3C/Keio
Keio University Shonan Fujisawa Campus
5322 Endo, Fujisawa city,
Kanagawa prefecture 252-8520 Japan
Telephone: +81-466-49-1170
Fax: +81-466-49-1171

A microformatted example company hCard, complete with address, and a sample rendering

By using fn together with org we can create an hCard for an organisation. Each part of the address is specified (<span>licious!), and we’ve also included a phone number (tel’s default type is voice) and fax number (specifying type and using the value class pattern).

A preview of how rich snippet data may be incorporated into Google’s search results—in this example “Kanagawa Prefecture” and “W3C” Microformat hCard data extracted by the Google Rich Snippet testing tool
Google’s Rich Snippet testing tool for an organisation hCard (note the address and organisation info in the search preview)

And with Operator, we can add this hCard to our computer’s address book in one click. If you’ve ever manually added an address to your address book, you’ll love this ;-)

Exporting an hCard to your computer’s address book via Operator An hCard exported by Operator is automatically added to the address book
Adding hCard-formatted contact information to your address book with one click

Using hCalendar for event information

Let’s briefly look at marking up a simple event. Here’s one I’m involved with:

<h3><a href="http://atnd.org/events/5181">WDE-ex Vol.11 —
  Designing for iPad: Our experience so far</a></h3>
<p>July 21st 19:00-20:00 at 
<a href="http://www.apple.com/jp/retail/ginza/map/">Apple Ginza</a>.</p>

Just by reading, we understand the event name, date, time, and location. But this information is difficult for computers to extract. By using the hCard microformat, it will be machine-readable.

<div class="vevent">
  <h3><a class="url summary" href="http://atnd.org/events/5181">
  WDE-ex Vol.11 — Designing for iPad: Our experience so far</a></h3>
  <p><span class="dtstart"><abbr class="value" title="2010-07-21">
  July 21st</abbr> <span class="value">19:00</span></span>-
  <span class="dtend"><span class="value">20:00</span></span> at 
  <a class="location url" href="http://www.apple.com/jp/retail/ginza/map/">
  Apple Ginza</a></p>
</div>

WDE-ex Vol.11 — Designing for iPad: Our experience so far

July 21st 19:00-20:00 at Apple Ginza

Example hCard-formatted event, including a sample rendering

We’ve added vevent on a wrapping element to indicate this is an hCalendar. Events are required to have a summary and a starting time, so we’ve added summary and indicated the starting date and time using dtstart. While historically datetimes were indicated using <abbr title="">, the required ISO 8601 format (for example “2010-07-10T19:01:29”) is very unfriendly to screen readers. Imagine having those numbers read to you! For this reason, the hCalendar specification recommends either breaking the datetime into separate date and time pieces, using the value class pattern, or possibly doing both. The specification is also smart enough to know that if the end datetime dtend is only a time, then it’s on the same day, so we don’t need to specify the date again, although tool support for this is spotty. Finally, we’ve added some URLs, one of which is for the event’s location.

The Firefox Operator plugin’s debug window, showing information about the current hCalendar
The Firefox plugin Operator’s debug view, showing the microformat properties it has recognised

With this data now being machine-readable, we can do things with it. For example, we can add the event to a calendar app with one click.

The Firefox Operator plugin’s options for hCalendar-formatted content
Operator can save hCalendar-formatted content as an ical file (for easy importing into your address book software), or add it to various online calendars

Using microformats in HTML5

So now we’ve had a whistle-stop introduction to microformats and gotten a glimpse of the benefits they provide. The question is: Can we use them in HTML5 pages? Let’s start with the good news.

As most microformats use only class and/or rel — basic parts of HTML that remain unchanged in HTML5 — these microformats are completely fine in HTML5. Yay! However there are a few wrinkles to keep us on our toes.

The profile attribute

The profile attribute is obsolete in HTML5, as it was determined unnecessary. Currently, microformat profiles are very rarely used in the wild and are not required, so this probably won’t affect you.

Microformats specifications vs HTML5

While microformats using class and rel work fine, some of the new features in HTML5 aren’t supported yet. The Microformats in HTML5 page on the microformats wiki currently begins:

This page is to document future use of microformats in HTML 5. None of the items documented are supported now, and may change upon proper development within the microformats community, or changes in the HTML 5 specification.

This includes the nifty new <time datetime=""> element, which would be perfect for microformat times, such as dtstart and dtend in the hCalendar microformat. It’s also true for adding microformats via microdata rather than class and rel. There has been talk of a general way to map any microformat into microdata, but this hasn’t eventuated as of yet.

Microformats-consuming tools (or, the problem with HTML Tidy)

The microformats wiki’s warning about <time> brings us to another caveat: tool support. Even if <time datetime=""> was valid in the microformats spec, at present most of the microformats tools can’t get this information from the datetime="" attribute. Now, you could wrap <time datetime=""> around a microformats dtstart or dtend:

<time datetime="2010-07-21T19:00:00+09:00"><span class="dtstart">
  <abbr class="value" title="2010-07-21">July 21st</abbr>
  <span class="value">19:00
</span></span></time>

However, as the <time datetime=""> data is then not explicitly part of the microformat, doing this isn’t really getting you anywhere, except bloated code-ville.

The problem is actually way worse as many tools use the parser HTML Tidy (“last updated March 2009”!), which probably won’t be fixed for new HTML5 elements. This means any microformats classes on new HTML5 elements will be ignored. The potential alternative html5lib.php is still pretty young. You can get around this by wrapping HTML5 elements in <div>s and <spans> and applying the microformats classes to them, but that’s hardly ideal.

However, there’s a ray of sunshine here. The tool you’ll most want to use is H2VX.com — it converts hCard and hCalendar microformats into vcard and ical files for users to download and add to their address books and calendars, respectively. While h2vx.com uses Tidy, Tantek Çelik and Brian Suda (the maintainers of H2VX) have a version of H2VX that does work with HTML5 elements and <time>’s datetime attribute. We’re saved! Check it out at dev.h2vx.com.

Microformats Tool support

OperatorOptimush2vx.comdev.h2vx.com
can use classes on HTML5 elementsyesyesnoyes
supports HTML5’s <time>nononoyes
Understands the value class patternyesnoyesyes
Understands implied dtendyesnoyesyes
Understands timezonesyesyesyesyes

I didn’t expect anyone to support <time> until it became part of a microformat specification. Given how … non-micro expressing datetimes is in microformats currently, I am very thankful H2VX has in dev.h2vx.com!

Conclusion

So where does that leave us? Well, if you keep the above caveats in mind you can safely use microformats in HTML5. Google has no problem with microformats in HTML5, even on new elements, and I’m under the impression that Yahoo is the same. However, remember that if you want to offer a “click to download” link via H2VX, you’ll have to use dev.h2vx.com.

An important thing to note about microformats: as wonderful as they are, the current method of using the plain old semantic HTML tools of class, rel, and coding patterns is something of a brilliant hack. The use of class (an author playground) and the lack of use/demise of profile, combined with the complexity of coding to detect for microformats in the wild, means that native browser support is unlikely anytime soon. There was the expectation that at some stage there’d be a better way to easily add extra semantic information for others to use. In future articles, we’ll look at microdata and RDFa, and see what they have to offer over plain old semantic HTML.

If you have common types of data in your content that are covered by a microformat, and you want to make them machine-readable for others (and who wouldn’t), you should definitely consider using microformats in HTML5. Adding microformats is basically adding a lightweight API to your content, and is simple enough that even a designer can do it! This helps search engines and savvy users to more easily use your content. Check out the microformats wiki, grab the indispensable microformats cheat sheet by Brian Suda, peruse these books and DVDs on microformats, and start getting (even more) semantic!

Books and DVDs

Changes

  1. Minor copyedits and the major addition of dev.h2vx.com support, with thanks and apologies for the delay to Brian Suda and Tantek Çelik.

Extending HTML5 — Microformats originally appeared on HTML5 Doctor on August 17, 2010.

]]>
https://stevefaulkner.github.io/html5doctored/microformats/feed/ 30
Your Questions Answered #6 https://stevefaulkner.github.io/html5doctored/your-questions-answered-6/ https://stevefaulkner.github.io/html5doctored/your-questions-answered-6/#comments Thu, 08 Apr 2010 14:30:59 +0000 http://html5doctor.com/?p=1578 HTML5. In this article, we'll discuss using multiple <h1>s, audio codecs, microformats, post bylines, and the <time> element.]]> Doctor treating a patient illustration We’re back with another round of patient questions about HTML5. In this article, we’ll discuss using multiple <h1>s, audio codecs, microformats, post bylines, and the <time> element.

Using multiple <h1>s

Bnonn asked:

I’m unsure of how h1 is supposed to be used now that pages can be divided into sections and articles. In (X)HTML4, a page was basically a page, and so you only had one h1 element: the top level heading for the page. I’m not even sure if that was a requirement of HTML4, or if it was just a de facto standard, but it made sense.

Now, however, there are any number of possible sections and articles per page; each of which may contain any number of heading elements (and one of those might be inside a header element). Given that h1 is a top-level heading, it seems as if each section and/or article should have one, in addition to the page-level h1. However, this contradicts what I know of accepted usage, so I’m confused about the way in which I should be marking up headings in HTML5. It doesn’t seem right to start headings in a section, for instance, with h2, because then…where’s the top-level heading for that element? But it doesn’t seem right to start them with h1, because that element in current web usage has more “power” than warrants its use there.

There is also SEO to wonder about. Even if using multiple h1 elements is right, I wouldn’t want to be penalized for it by Google. So what’s the story with that?

Any help is much appreciated.

Regards,

Bnonn

Basically, you can use multiple <h1>s in order to shape the document outline. So an <h1> within a <section> will be treated like an <h2> (if it is a nested <section>). If that <section> (and only that <section>) is aggregated elsewhere on the web, however, it will fit seamlessly into that external document’s outline. This means that your original <h1> could be treated as an <h1>, <h2>, <h3>, or <h4> as appropriate.

I hope that makes sense! Bruce’s article on the section element touches on this, and we also hope to talk more about document outlines in the future.

Cheers,

Rich

P.S. Multiple <h1>s in HTML4 are perfectly legal, but seldom used.

Audio Codecs

Adam asked:

Hello,

I’ve got a query about the issues regarding what codecs to use for the audio tag. I don’t quite get why Firefox won’t support mp3. Surely the browser calls on the codecs present on the operating system, not within the browser, so how are the licensing issues a problem to the browsers if they are simply leaving what is already present (and in theory already licensed)?

I fully admit that I am no lawyer. It just seems very odd the line of argument Mozilla has taken.

Adam Wilcox

I’m not sure why they’ve taken that stance, but I know that Mozilla only plans to support Ogg and Apple are keen to push MP3 and MP4, so we’re at stalemate. Because of this, discussion of specific codecs has been removed from the spec altogether.

Although not related to audio, see this article I wrote about YouTube and Vimeo supporting HTML5 video. It explains a little about codecs.

Sorry I can’t be of more help.

Rich

Since writing this reply there has been a lot more talk over codecs for both video and audio since Microsoft announced they would be supporting H.264 in IE9. We’ll cover this in more detail soon.

hCards

Ken asked:

How does html 5 deal with hCards and microformats in general? Specifically, can you have more than one hCard per page? As I understand it currently, having more than one is problematic. I’m asking now because of the treatment “footer” and “aside” has gotten, i.e. they are context sensitive to their “article” or “section” unless top-level, then applied to the page. Also, the use of scoped on style tags: hCards should be updated in a like manner for html 5.

Thanks for hearing me out. I don’t know if this is the appropriate place for this question, but as html 5 is much more useful, I thought maybe a post is in order ;)

Also, “article”‘s impact on the hAtom microformat makes for an interesting article, don’t ya think?

We touched upon microformats in our articles on the <address> and <time> elements, but we’ll try to cover more soon (possibly in an article on the <article> element).

I haven’t heard that having more than one hCard is problematic. I’ve certainly never had any issues doing that. If you check out the source code on the homepage of HTML5Doctor, you’ll see we’ve used multiple hCards — one for each author’s name associated with a post.

Hope that helps a little.

Cheers,

Rich

Article bylines

Eric asked:

I tweeted to you guys a few days ago (@ericdfields), wondering what you recommend for bylines for blog posts. Bylines, borrowing a newspaper term, would be the ‘Posted by SoandSo on Nov 1, 2010’ copy you find under the article of your typical blog post.

To me, they seem like something of an ‘aside’ for the header of the document.

I don’t know if its perfectly valid, but here’s the test case I whipped up.

Thanks in advance for any insight!

In my opinion, they’re header or footer information, so I do:

<header>
  <hgroup>
    <h1>Foo</h1>
    <h2>Bar</h2>
  </hgroup>
  <p>Posted by Bruce on <time datetime="2009-11-01">1st November</time></p>
</header>

Thanks, Bruce

How to use <time> with a date in astrology

Dear HTML5Doctors,

I was confused in how to using the time tag with the Astrology date. For example if I have a chunk of content like this:

ARIES: 13 Apr – 14 May

how can I use attribute “datetime” to explain the format of that date?

<p>ARIES: <time title="start" datetime="???">13 Apr</time> - <time title="end" datetime="???">14 May</time></p>

The year of birth is not fixed, please advice me some solutions about this.

Thank you,

radiz

Hi Radiz,

The time element is for one specific day, not a range or “imprecise” date. So you can’t use it for “13 April.” You need to use it for a specific “13 April” and give a year. For more, see our article on the <time> element.

Thanks, Bruce

Got a question for us?

That wraps up this round of questions! If you’ve got a query about the HTML5 spec or how to implement it, you can get in touch with us and we’ll do our best to help.

Your Questions Answered #6 originally appeared on HTML5 Doctor on April 8, 2010.

]]>
https://stevefaulkner.github.io/html5doctored/your-questions-answered-6/feed/ 4
The time element (and microformats) https://stevefaulkner.github.io/html5doctored/the-time-element/ https://stevefaulkner.github.io/html5doctored/the-time-element/#comments Tue, 09 Feb 2010 14:49:14 +0000 http://html5doctor.com/?p=1293

Related Posts:

]]>
Please note that since this was written, <time>, datetime have been made more powerful, so this article is obsolete. Doctor Bruce has the low-down in his blogpost The best of <time>s.

Microformats are a way of adding extra semantic information to a webpage using HTML classes — information like an event’s date and time, a person’s phone number, an organisation’s email address, etc.

They aren’t a “standard” per se, but they are a widely adopted convention within the geek community. And since they use an agreed-upon set of class names, they are as compatible with HTML5 as they are with HTML4 or XHTML.

HTML5 offers one potential addition to the microformats arsenal. Because earlier versions of some microformats presented accessibility problems in the way they encoded dates and times (see an article called hAccessibility that I co-wrote with James Craig), HTML5 offers a new element for unambiguously encoding dates and times for machines while still displaying them in a human-readable way.

The time element represents either a time on a 24 hour clock, or a precise date in the proleptic Gregorian calendar, optionally with a time and a time-zone offset.

The format is very simple:

<time>2009-11-13</time>
 <!-- without @datetime content must be a valid date, time, or precise datetime -->
<time datetime="2009-11-13">13<sup>th</sup> November</time>
 <!-- when using @datetime the content can be anything relevant -->
<time datetime="20:00">starting at 8pm</time>
 <!-- time example -->
<time datetime="2009-11-13T20:00+00:00">8pm on my birthday</time>
 <!-- datetime with time-zone example -->
<time datetime="2009-11-13T20:00Z">8pm on my birthday</time>
 <!-- datetime with time-zone “Z” -->

When using the datetime attribute you can have whatever you want between the tags.

Note that the last two examples show the use of a timezone offset, which is required for a date + time. Timezones range from -12:00 to +14:00. You can use “Z” to represent Coordinated Universal Time (UTC), which is the same as +00:00.

The fly in the ointment

The only trouble with <time> is that it must contain a positive date on the Proleptic Gregorian calendar, meaning you can’t encode a date before the Christian Era. Neither can you encode imprecise dates such as “July 1904”. Microformats, however, aren’t subjected to this restriction and are often employed to mark up, for example, very old events on Wikipedia or imprecise dates on museum websites. It’s likely that authors will continue using the current, more flexible microformat patterns unless the restrictions on the <time> element are lifted. (The HTML5 working group has been continually petitioned to remove their arbitrary restrictions and widen the scope of time, but with no success).

Unfortunately current microformat clients can’t consume microformats such as hCalendar if they’re coded to use <time>. Stefan Schipor wrote to us saying:

The problem is that I need to parse the site with H2VX or any other similar thing for exporting. Sadly, H2VX doesn’t seem to read the datetime attribute in the <time> elements. Nor does the FF add-on Tails. I tried the microformats IRC channel and the microformateers twitter account but I didn’t get any response. Do you know if there is any support for this? It would kill me to add <abbr> elements just for it to work.

For more on this problem refer to Oli’s comment below, and our article Extending HTML5 — Microformats.

Indicating publication date

In previous drafts of the spec, there was a pubdate attribute to show that this particular date is the publication date for the text associated with it. This was removed. It’s generally a good practice to add a date to articles, so users can judge its relevance. If you want to expose that with markup, you can use schema.org’s BlogPosting schema and HTML5 microdata.

My personal site is marked up:

<article role="article"
  itemscope itemtype="http://schema.org/BlogPosting">
<header>
<h2>Heading</h2>
<time itemprop="dateCreated" datetime="2013-10-30"> Wednesday 30 October 2013</time>
</header>
…
</article>

Where could time be used?

The uses of unambiguous dates in web pages aren’t hard to imagine. A browser could offer to add events to a user’s calendar. A Thai-localised browser could offer to transform Gregorian dates into Thai Buddhist era dates. A Japanese browser could localise <time>16:00</time> to “16:00時”. Content aggregators can produce visual timelines of events.

Search engines can produce smarter search results. For example, in the recent heavy snow in the UK, I searched for school closures in my town and discovered my kids’ school was closed. After receiving a phone call from the school asking me where my kids were, I re-examined the webpage. In small print at the bottom of the page were the words "Published 5 January 2008". I expect that operators of search engines would rank more current pages more highly on searches connected with current events.

The time element (and microformats) originally appeared on HTML5 Doctor on February 9, 2010.

]]>
https://stevefaulkner.github.io/html5doctored/the-time-element/feed/ 78