Html страницы текущее время

A Guide to the HTML5 ‘time’ Element

Time – one of the few known things that is infinite. Human beings, as well as animals and plants, have dealt with time since the beginning of their existence.

On the web this need isn’t different. Even in this medium, we need to communicate to other people that something happened at a certain point, on a specific date, or in relation to another set time.

Prior to HTML5 we had no element to semantically mark up a date or time. In recent years, other solutions, most notably Microformats and Microdata, have tried to fill this gap for specific situations (date of birth, publication of a book, and so on).

In this article I’m going to cover HTML5’s element, which helps address the need just discussed.

What’s the Element?

The element was introduced in the HTML5 spec in 2009. It was then dropped in 2011 in favor of . Then was reintroduced, and then improved to allow new date/time formats. From this you can see that specifications can be quite controversial.

An example is shown below:

In the code above, I’m defining a date, specifically February 1, 2009. The format employed in the code ( yyyy-mm-dd ) should be familiar to you if you have spent some time on Linux but, as we’ll see later in this article, this isn’t the only valid format.

Читайте также:  Java time code example

In the first draft of the specifications, precise dates were one of the few formats you could write. For example, you couldn’t specify a date like “November 2014” or “476” (the start of the Middle Ages). This was a big issue for several cases like the dating of a painting or a historical event because a precise date isn’t known.

Fortunately, this type of date is now permitted in the spec. So today we can describe a given month of a year without a day:

The datetime Attribute

The specification for the element also standardized an attribute called datetime .

While writing dates in the formats discussed in the previous section works in some countries/cultures, it doesn’t suit others. For example, Italians write dates using the format dd/mm/yyyy . Therefore, showing a date in another format may lead to confusion.

This issue can be easily solved using the datetime attribute of the element. It’s an optional attribute that contains the information in a machine readable format, like those seen in the examples, allowing us to write the content of the element in any way we like.

In fact, if datetime isn’t specified, the content must be in one of the valid date/time formats, otherwise we can use it as we want. This is great because it allows us to write code as follows:

The next meeting is scheduled for time datetime="2014-10">Octobertime>.
The next meeting is scheduled for time datetime="2014-10">next monthtime>.

Both these examples have date content that isn’t machine readable according to the specification, but they are acceptable because of the presence of the datetime attribute, which does use a valid format.

At first glance, this might seem odd. But the content of the element has been designed to serve human beings, not machines. Besides, this fact allows for the internationalization of the dates. For example:

 Il prossimo incontro è programmato per time datetime="2014-10">il mese prossimotime>.

In the code above, we have the same message as before, but in Italian.

The pubdate Attribute

The first drafts of the specification defined a pubdate attribute for the element. This attribute was a Boolean indicating that a given date was the publication date of the parent element or, if in the absence of a parent element, of the whole document.

For example, you could write:

article> h1>A good titleh1> p>This is the content of a great article.p> footer> p>Article published on time datetime="2014-09-05" pubdate>September the 5th, 2014time> footer> article>

In this case, September 5, 2014 would be the publication date of this “article”.

I’ve been a great fan of this attribute since I learned about it but, unfortunately, it was removed from the spec. This decision has created a big problem because a lot of people (including me) use the publication date to judge the freshness and the relevance of an article or news piece. While it’s true that you can still access the page of an article and view the publication date, we need a standard way for a machine to read the date.

At the moment there isn’t an attribute that replaces pubdate , but you can employ the BlogPosting schema, and specifically the datePublished value, as illustrated below:

article itemscope itemType="http://schema.org/BlogPosting"> h1 itemprop="headline">A good titleh1> p itemprop="articleBody">This is the content of a great article.p> footer> p>Article published on time datetime="2014-09-05" itemprop="datePublished">5th September 2014time> footer> article>

Now that you have a complete overview of the element, let’s see the several formats allowed.

The valid formats for the content of the element in absence of the datetime attribute, and for the datetime attribute itself are described in the following sections.

A Valid Month

This should be a string specifying a specific month of a year in the form of yyyy-mm . For example:

A Valid Date (Day of the Month)

This should be a string specifying a precise date in the form of yyyy-mm-dd . For example:

A Valid Yearless Date

This should be a string specifying a month and a day without a year in the form of mm-dd . For example:

A Valid Time

This should be a string specifying a time without a date and using the 24-hour format, in the form of HH:MM[:SS[.mmm]] where:

  • H stands for hours
  • M stands for minutes
  • S stands for seconds
  • m stands for milliseconds
  • The square brackets indicate the parts that are optional

An example of this format is shown below:

 time datetime="16:10">afternoontime>

A Valid Floating Date and Time

This format is present in the W3C spec, but not in the WHATWG version. This should be a precise date and time in the format yyyy-mm-ddTHH:MM[:SS[.mmm]] or yyyy-mm-dd HH:MM[:SS[.mmm]] . For example:

 time datetime="2014-09-16T18:20:30">Tuesday at 18:20time>

A Valid Time Zone Offset

This should be a string representing a time zone offset. For example:

A Valid Global Date and Time

This should be a string representing a complete date, including time and time zone. For example:

 time>2014-09-16T18:20:30+01:00time>

A Valid Week

This should be a string representing a week of a year. For example:

A Valid Year

This should be a string representing a year. For example:

 time datetime="2014">this yeartime>

A Valid Duration String

This should be a string representing a duration. A duration can start with the prefix “P”, standing for “period”, and use a “D” to mark days. For example:

 time datetime="P4D">four daystime>

In case you need to further specify the period, after the “D” you can add a “T”, standing for “time”, and use “H” for hours, “M” for minutes and “S” for seconds. Like this:

 time datetime="P4DT4H3M">four daystime>

This format also allows you to specify one or more duration time components.

Limitations

The current specification has some limitations in what you can define with the element. One of these limitations is that you can’t indicate date ranges. So if you are writing a post about a conference that lasts more than one day, for example from June 26, 2014 to June 28, 2014, you have to use two elements. A good example can be found in the speaking page on my website where I use the element as shown below:

time datetime="2014-06-28">26span class="hidden">June 2014span>time>-time datetime="2014-06-28">28 June 2014time>

Another limitation is that you can’t use the element to represent dates before the Common Era.

Browser Support

Based on the element article on MDN, this element is supported in:

  • Chrome 33+
  • Firefox 22+
  • Internet Explorer 9+
  • Opera 22+
  • Safari 7+

However, there is not much to worry about in older browsers. In fact, in case it lacks support for this element the browser will simply display it as an unknown inline element.

Conclusion

If you haven’t started using the element in your pages, I hope this guide has inspired you to do so.

For more info, here are some relevant links:

This article is also available in Portuguese on Tableless

Share This Article

I’m a (full-stack) web and app developer with more than 5 years’ experience programming for the web using HTML, CSS, Sass, JavaScript, and PHP. I’m an expert of JavaScript and HTML5 APIs but my interests include web security, accessibility, performance, and SEO. I’m also a regular writer for several networks, speaker, and author of the books jQuery in Action, third edition and Instant jQuery Selectors .

Источник

: The (Date) Time element

The 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.

It may represent one of the following:

  • A time on a 24-hour clock.
  • A precise date in the Gregorian calendar (with optional time and timezone information).
  • A valid time duration.

Try it

Attributes

Like all other HTML elements, this element supports the global attributes.

This attribute indicates the time and/or date of the element and must be in one of the formats described below.

Usage notes

This element is for presenting dates and times in a machine-readable format. For example, this can help a user agent offer to add an event to a user’s calendar.

This element should not be used for dates prior to the introduction of the Gregorian calendar (due to complications in calculating those dates).

The datetime value (the machine-readable value of the datetime) is the value of the element’s datetime attribute, which must be in the proper format (see below). If the element does not have a datetime attribute, it must not have any element descendants, and the datetime value is the element’s child text content.

Valid datetime values

a valid yearless date string

a valid local date and time string

a valid global date and time string

Источник

Оцените статью