Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Working with date and time

From: HTML5: Structure, Syntax, and Semantics

Video: Working with date and time

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.

Working with date and time

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.

Show transcript

This video is part of

Image for HTML5: Structure, Syntax, and Semantics
HTML5: Structure, Syntax, and Semantics

46 video lessons · 36595 viewers

James Williamson
Author

 
Expand all | Collapse all
  1. 2m 20s
    1. Welcome
      48s
    2. Using the exercise files
      1m 32s
  2. 18m 41s
    1. A brief overview of HTML5
      3m 57s
    2. What's in the HTML5 specification?
      8m 17s
    3. Why do we need new structural elements?
      6m 27s
  3. 50m 33s
    1. Defining HTML5 documents
      5m 5s
    2. HTML5 syntax
      9m 14s
    3. The header element
      5m 22s
    4. The nav element
      4m 55s
    5. The section element
      4m 51s
    6. The article element
      4m 48s
    7. The aside element
      4m 13s
    8. The footer element
      4m 17s
    9. Content model overview
      7m 48s
  4. 35m 28s
    1. Understanding the outline algorithm
      3m 17s
    2. Creating document sections
      8m 25s
    3. Using headings properly
      9m 1s
    4. Using hgroup to override sectioning
      4m 17s
    5. Properly nesting structure
      7m 17s
    6. Sectioning roots
      3m 11s
  5. 58m 30s
    1. Organizing content
      4m 41s
    2. Planning document structure
      5m 47s
    3. Choosing the right structural element
      4m 43s
    4. Checking document outlines
      5m 27s
    5. Coding initial page structure
      5m 28s
    6. Using class and ID attributes
      5m 31s
    7. Structuring headers
      13m 13s
    8. Building navigation
      7m 1s
    9. Structuring footers
      6m 39s
  6. 1h 27m
    1. Working with figure and figcaption
      7m 12s
    2. Grouping content with asides
      3m 46s
    3. Using divs in HTML5
      5m 0s
    4. Working with lists in HTML5
      7m 10s
    5. The return of bold and italic
      5m 52s
    6. Citing works semantically
      6m 32s
    7. Using the address element
      5m 24s
    8. Using the small element
      4m 24s
    9. Using the mark element
      5m 16s
    10. Working with date and time
      11m 55s
    11. Creating block-level links
      8m 53s
    12. Understanding link relationships
      9m 28s
    13. Defining link relationships
      6m 23s
  7. 17m 22s
    1. Current browser support
      7m 38s
    2. Ensuring block-level display
      4m 3s
    3. Adding support for elements in older browsers
      5m 41s
  8. 3m 46s
    1. Additional Resources
      3m 46s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed HTML5: Structure, Syntax, and Semantics.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.