Viewers: in countries Watching now:
Gain a deeper understanding of HTML5 and learn how to create richer, more meaningful web pages with structural tags and descriptive attributes. In this course, author James Williamson presents an overview of HTML5 and its development, defines the new tags and attributes, and discusses how browsers parse and display HTML5 content. The course also includes step-by-step instructions for constructing an HTML5 document with a header and footer, navigation, content groups, and formatting.
In the past, there was never a consistent way for marking up time and dates. As a result authors would typically use class attributes to identify things such as publishing dates and significant events. Now, this lack of standardization made it extremely difficult to generate machine-readable dates and times. Now, to address this, HTML5 introduces the time element, which allows you to represent time based on a 24-hour clock or a precise date from the Gregorian calendar. Now, so here I am in the spec, and I'm looking at the time element, so section 4.6.10.
If you go to the Text Level Semantics, you will find the time element. And there are a couple of things that I want to point out about this. First, we do have some restrictions. The content model is phrasing content, so it's considered inline content, but there must be no time element descendants, so you can't mark up time with the time element. That kind of makes sense if you think about it. We do have two attributes that are specific to time: they're datetime and pubdate. Now, we'll talk more about those in just a moment; for right now let's focus on what the definition tells us about the time element. Okay. So the time element, as I mentioned before, represents time on either a 24-hour clock or a precise date in the Gregorian calendar.
There are some issues with that being part of the Gregorian calendar because it means that some of those ancient times can't be recorded using the time element. So there is still a little bit of discussion going on as to whether the specification should be changed about that. Now, notice again the definition says, "This element is intended as a way to encode modern dates and times in a machine-readable way so that user agents can add things like birthday calendar reminders or scheduled events to the user's calendar." Notice that the time element is not intended for encoding times for when a precise date or time cannot be established.
So if you said something like, again, one millisecond after the Big Bang or the early part of the Jurassic period, obviously the time element would not be used for those. Or even if you said the second Monday of next year, that probably would not be used for that. Now, I'm going to scroll down and talk about these attributes that you can use with the time element: the pubdate and you have datetime. Let's focus on pubdate first. Now, pubdate is a Boolean attribute, meaning if it's present, it's true. And of course it stands for publishing date. So essentially, it's indicating that the date and the time that's given by the time element for the nearest ancestor is the date that that article or section was published.
So, obviously this is only going to be used with specific types of elements or specific types of content. So articles that are being published, blog posts, comments, things like that where you're basically stamping the date that that was published, the pubdate attribute is used for that. The datetime attribute is an extremely useful attribute inside the time element. Essentially, if it's present, you can go ahead and pass along a specific date and/or time into the attribute. And then anything that's marked up with the time element, that date or time will apply to that.
And that allows you to be kind of loose with what you're writing. So you could write something like 'on July 29.' Well, you might not format the text in your code in a machine-readable way, but datetime allows you to pass the machine-readable code for June 29 into your code. So it's extremely useful, and you'll see how we're going to use it in just a moment. Now, this next section down here, where it says, "If the element needs a date... If the element needs a date," this can be a little confusing. So let's talk about what the spec is saying here. It's saying that if the time element does not have a datetime attribute, then the contents of the time tag must be a valid date string, and we're going to talk about what a valid date string is in just a moment.
Now, if it does have the datetime attribute, then the actual contents of the time element can be in any format that you want, but the datetime attribute itself must be, again, in a valid date string. So again, that's the option that the datetime attribute gives to us when we use it. Now, honestly, this is a little bit easier to visualize when we actually do it, so we're going to go back in our trails page and use the time element, and you can kind of see what we're talking about. All right! So we have a few things on our page that I want to mark up with the time element, and I want to start in our Comments section-- that's a logical place to start.
So I'm going to go scrolling down until I find our comments, and there is our first comment. You'll notice that we have Max Smith wrote this on April 25th, 2011 at 12:52. So, very specific here, and we're going to mark this up with the time element. Okay. So the first thing I'm going to do is right inside of April 25th, I'm going to wrap that with a time element. So I'm going to do time and then wrap that. Now, currently this is nonconforming, because even though this is really specific to you and me, April 25th, 2011 at 12:52, it's not a valid date string.
So if the text in our code isn't a valid date string, we can still go ahead and pass along a valid date string using the datetime attribute, which we're going to do. All right! So the first thing I'm going to do is I'm going to do a datetime attribute, so datetime. And now I need to pass along a valid date string. So what's a valid date string? All right. Well, let's just go ahead and type one in. We pass the year first, so that's 2011. Then I'm going to do a dash. Then we pass the month, which is 04 for April, and then I'll do another dash, and that will be 25.
Now, in most cases, your blogging software, or whatever software you use to publish this, should do this for you. So I doubt very seriously you're going to have to do a lot of this. It's helpful to do it by hand though, because you really get to see what all this means. Now, when you pass a date like this-- this is a valid date string--you also have the option of passing the time. And since we did mention the time here in our code, it's good enough to go ahead and pass our time here. So I'm just going to do a T; T is the indicator that time follows this. And the time is in the 24-hour format, so this is going to be 12:52.
And then you can pass seconds and milliseconds if you want. So let's just do seconds, so 04 for seconds. And then you can also pass along a time zone offset. Essentially, that tells the machine that's reading this what time offset from Greenwich Mean Time this is. Since this is published on the West Coast, we're going to give it a time offset of eight hours, so 08:00. Okay. Wow! Again, thankfully, most of the time you're going to have machines to do this for you.
But if you need to do this for yourself, it's helpful to understand where all this stuff comes from. So I'm going to save the file. I'm going to go back out to my browser for just a moment so we can shed a little bit more light on what a valid date string is. Okay. So when you're looking at the table of contents for the HTML5 Edition for Web Authors, find the section called Common microsyntaxes. You can find in that this little section here, Dates and times. And if you click on that, it's going to give you all the formatting that you'll need to know in order to do a valid date string.
It's going to show you how dates and times are represented, how to represent a month, dates, time, and it goes through local dates and time, global dates and time, so it gives you all of the formatting options that you're going to need for those. Now, the one thing that I was really confused about when I started marking up my own dates and times was, how in the world do I know what my time zone offset is? I mean, sure, I know if I'm posting on the East Coast, maybe I'm five hours behind Greenwich Mean Time, but you want to be sure about that, right? So let me show you a great little resource.
If you go to Wikipedia--what a wonderful resource Wikipedia is, right? And if you type in, in the search for Wikipedia, 'utc time zone offsets', you're going to get a couple of results, and I'm going to show you the one that you want. The one you want is the Time zone section, and it has a subsection that says Time zone as offset from UTC. Now, if you click on that--this is wonderful!--you get a great little table here of every single region of the world and what the time zone offset is going to be. And the other nice thing about this is it shows you this in the context of a valid date string.
So you get to see exactly how this should be formatted. So if you're in the business of writing the software that's going to be formatting this, or if, heaven help you, you need to do this by hand, this is a great little resource to have. So go ahead and bookmark that and you'll know where to find this. Okay. So now that we know all of that, there's a couple of more things that I need to do here in our code. One thing that I want to do to this one in particular is, since this comment has been published at a certain time, it really would benefit from having the pubdate attribute. So right after time, I'm going to go ahead and do pubdate.
And this is a Boolean attribute, so you don't need to say pubdate=pubdate. You can just say pubdate, just like that. So we have a few more date times that I want to do really quickly. I'm going to scroll down, and I can see that down here in the rider-submitted reviews we have some time and when that time it was submitted. So now, again, what I'm going to do is I'm going to surround the date here with a time element. I'm going to go ahead and give it a datetime attribute. And I'm going to make that equal to, and in this case, I'm going to do 2010-- I've got to take a look at the date that I'm actually marking here-- -02 for the month, -23 for the date.
I'm going to close that, and of course this does get the pubdate attribute as well. And then I'll finish wrapping the time tag. Now, again, here this is a much shorter date string, but it's still a valid date string. Remember, time is optional. You don't have to pass time. So again, I'm going to go down to my next rider review, the one Copper Canyon Loop by Shea Hansen, and I'll just do the same thing here. And I'm just going to go ahead and wrap this in a time element. Sometimes I'll just go ahead and do the element first and then come back and do the attributes; other times I'll do the attributes along with it.
And so here, again, I want to do datetime, and the valid date string here is going to be 2011-03-11. Wonderful! Go ahead and save that. Now, we're almost done. We have one more left to do. And the reason I saved this one for last is I wanted to show you kind of how ambiguous you can be with this. I'm going to scroll up a little bit further to right around line 78 or so, and we can see that in this paragraph of text, we say, "I also want to mention that on June 22 of every year, the Ojai Biking Club sponsors a trail cleanup day." Now, I'm not insinuating by marking this one up that every single date or every single time that you mention needs a time attribute.
But think about this. If we could set this to be machine- readable so that somebody could click on this, for example, and set a calendar reminder, that would be really cool. All right! So in that instance, the time element would really make a lot of sense here. So I'm just going to go ahead and do another time element, and I'm going to do it around the entire date. And this one is going to get a datetime attribute as well, because it's not a valid date string. And we're just going to do 2011-06 for the month, -22.
And this one doesn't need a pubdate attribute because it's not a publishing date, it's just marking a specific time. All right! Now I'm going to go ahead and save this. And of course, if you preview this in your browser, you shouldn't notice any change in the code whatsoever. So even down here where we're talking about the date, we're talking about the publishing dates here, this has nothing to do with visuals at all. There's no physical change in our dates. Browsers simply ignore the time element in terms of rendering how they present it. What it does do for us is it makes those dates and times available in a consistent machine-readable format.
Now, the date syntax can be a little tricky, so if you're not familiar with the valid date string syntax, make sure you go back and reference the microsyntax section of the HTML5 specification the first couple of times that you use it. Also, I want to mention one more time before we move on, don't forget that you don't need the time element to mark up every date and time. You only need it when you want to convey the date and time in a way that applications, aggregator, search engines, and other content readers can understand.
There are currently no FAQs about HTML5: Structure, Syntax, and Semantics (2011).
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.