Click here to Skip to main content
15,891,951 members
Articles / Programming Languages / Javascript

Expressing Date/Time Information with the <time> Element

Rate me:
Please Sign up or sign in to vote.
4.60/5 (3 votes)
29 Jan 2015CPOL2 min read 10.7K   3   2
Using the HTML5 element allows to express a human-readable date or a timestamp in a web page, and annotate it with a machine-readable date/time value that can be extracted and processed by applications.

While date/time information items have to be formatted as strings in a human-readable form on web pages, preferably in localized form based on the settings of the user's browser, it's not a good idea to store date/time values in this form. Rather, we use instances of the pre-defined JavaScript class Date for representing and storing date/time values. In this form, the pre-defined functions toISOString() and toLocaleDateString() can be used for turning Date values into ISO standard date/time strings (of the form "2015-01-27") or to localized date/time strings (like "27.1.2015"). Notice that, for simplicity, we have omitted the time part of the date/time strings.

In summary, a date/time value is expressed in three different forms:

  1. Internally, for storage and computations, as a Date value.

  2. Internally, for annotating localized date/time strings, or externally, for displaying a date/time value in a standard form, as an ISO standard date/time string, e.g., with the help of toISOString().

  3. Externally, for displaying a date/time value in a localized form, as a localized date/time string, e.g., with the help of toLocaleDateString().

When a date/time value is to be included in a web page, we can use the <time> element that allows to display a human-readable representation (typically a localized date/time string) that is annotated with a standard (machine-readable) form of the date/time value.

We illustrate the use of the element with the following example of a web page that includes two elements: one for displaying a fixed date, and another (initially empty) element for displaying the date of today, which is computed with the help of a JavaScript function. In both cases, we use the datetime attribute for annotating the displayed human-readable date with the corresponding machine-readable representation.

HTML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <meta charset="UTF-8" />
 <title>Using the HTML5 Time Element</title>
 <script>
function assignDate() {
  var dateEl = document.getElementById("today");
  var today = new Date();
  dateEl.textContent = today.toLocaleDateString();
  dateEl.setAttribute("datetime", today.toISOString());
}
window.addEventListener("load", assignDate);
 </script>
</head>
<body>
 <h1>HTML5 Time Element</h1>
 <p>HTML 2.0 was published on 
    <time datetime="1995-11-24">24.11.1995</time>.</p>
 <p>Today is <time id="today" datetime=""></time>.</p>
</body>
</html> 

After this web page is loaded, the JavaScript function assignDate() is executed. It computes today's date as a Date value and assigns the ISO standard representation to the element's datetime attribute and the localized representation as the text content of the element.

Read more about web engineering on web-engineering.info.

This article was originally posted at http://web-engineering.info/blog-feed.xml

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Instructor / Trainer
Germany Germany
Researcher, developer, instructor and cat lover.

Co-Founder of web-engineering.info and the educational simulation website sim4edu.com.

Comments and Discussions

 
QuestionHTML 5 New Elements Pin
Shadat Tonmoy1-Feb-15 22:16
Shadat Tonmoy1-Feb-15 22:16 
AnswerRe: HTML 5 New Elements Pin
Gerd Wagner2-Feb-15 7:12
professionalGerd Wagner2-Feb-15 7:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.