New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

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

Working with dates

From: JavaScript Essential Training

Video: Working with dates

Unlike many other programming languages, JavaScript doesn't have a lot of built-in objects for us to try and remember. It has the Math object we can directly use. It has an Array object. One of the others is the Date object. This is how we make a new date. Now, while you could of course just make several variables to hold a year, month, and date yourself, this one is a bit more useful. So we use the var today--pick your own variable name--equals the word new and Date, with an uppercase D and using parentheses. Now, if you don't pass any parameters, you'll get it returned with the current date and time.

Working with dates

Unlike many other programming languages, JavaScript doesn't have a lot of built-in objects for us to try and remember. It has the Math object we can directly use. It has an Array object. One of the others is the Date object. This is how we make a new date. Now, while you could of course just make several variables to hold a year, month, and date yourself, this one is a bit more useful. So we use the var today--pick your own variable name--equals the word new and Date, with an uppercase D and using parentheses. Now, if you don't pass any parameters, you'll get it returned with the current date and time.

Notice that, it's not just the date; it's the time. It's actually the time down to the millisecond. Now, you don't always want a date object that exists for today. So you could create another one which is a new Date and then passing in year, month, and day. But notice month is 0 based like an array; it goes from 0 to 11. The year is the real year, the day is the real day 1 through 31, but month is 0 based here. You can also get it down to, in this case, seconds, year, month, day, hours, minutes, seconds if you want to be very specific.

Internally, a date object is stored as the number of milliseconds since the 1st of January 1970, which is a pretty common way of doing it in the programming world. Once you have a date object, like having an array or like having a string, you can do things with it. We have a lot of methods of the date object that begin with the word Get. So after creating a new object, I can say today.getMonth and it returns 0 through 11. I can say today.getFullYear to give me the four-digit version.

That one is not 0 based, so it would return 2011, 2012, and so on. There is actually a today.getYear, but that one is officially deprecated, meaning that it would work, but you're not supposed to use it anymore. The recommended one is getFullYear. We have today.getDate. Now, for those of you coming from things like SQL, that might sound like something that should return the full date, but no. Here that will actually return you the day of the month, and it's 1 through 31, as opposed to today.getDay, which would return 0 through 6 for the day of the week and 0 is the Sunday.

An example of using this is if I define say a new Date, and here I'm doing a date that is 1906, December because we're 0 based for the month, and the 9th. That's actually Grace Hopper's birthday or Grandma COBOL. So I'm going to then write out console.log, give it a string, then a comma, and then I will call myDate.getDay(), opening and closing parentheses just to say I'm passing no parameters there.

I then close the parentheses of the console.log call and finish the statement, rerun that. Apparently, Grace Hopper was born on a Sunday, and simple silly example there, but that's how you start to get into the deeper parts of the Date object. We even have today.getHours. This one is also 0 based, 0 through 23, and there's today.getTime. Now, this one will actually return the milliseconds since the 1st of January 1970. Now, that sounds like it's a little bit esoteric, but in fact getTime is one of the most useful methods of the Date object, and you'll probably see it used quite a lot, because it's a very direct way to figure out, is something less than something else? If one date is less than other one, I don't have to bother comparing years and months and days, if I can just get the big number that represents the milliseconds.

Now conversely, we have all these Get methods. We also have Set methods. So where we have getMonth, we also have setMonth where we can pass in the number of the month, set for year, setDay to manipulate those dates. Again, you can find the full description of these in any online JavaScript reference. One thing we'll often have to do is compare dates, and this does not work the way most people think it does. Let's say I create two new date objects. I'm setting them both here to the 1st of January and the year 2000.

Again, we're passing a 0 for the month, because the month is 0 based here, and then I ask, is date1 equal to date2? And I'm using the double equals here. Now, this would actually respond as false. Now, as whether I use double equals or triple equals, that's not the problem here. The problem is is the dates are object, and they're a bit more complex. What JavaScript is doing is looking at and going, are these the same object? Well, no, they're actually not. They may internally have the same values as we're concerned, but it's not going that deep.

It's realizing there are two different objects and saying this is false. We need a simpler way to compare them, the way that we can compare say numbers, which is very easy. So what I'll do instead is I'll ask if date1.getTime-- that's that really useful method that just returns milliseconds--is equal to date2.getTime. Now, calling both of these are just going to return milliseconds. It would be like comparing two numbers, and this would return true. Now you can also use this very easily with comparisons such as greater than, less than, greater than or equal to, less than or equal to.

So this is one of those areas where the methods of the Date object come in very handy indeed if you don't want to have to manually start comparing year parts and month parts and day parts.

Show transcript

This video is part of

Image for JavaScript Essential Training
JavaScript Essential Training

56 video lessons · 99541 viewers

Simon Allardice
Author

 
Expand all | Collapse all
  1. 3m 28s
    1. Welcome
      1m 1s
    2. What you should know
      1m 44s
    3. Using the exercise files
      43s
  2. 15m 41s
    1. Introduction to JavaScript
      8m 6s
    2. Creating your first JavaScript
      2m 13s
    3. Getting to know the tools and applications
      5m 22s
  3. 56m 8s
    1. Understanding the structure of JavaScript code
      7m 9s
    2. Where to write your JavaScript
      3m 56s
    3. Creating variables
      6m 21s
    4. Working with conditional code
      5m 44s
    5. Working with operators
      13m 28s
    6. Sending messages to the console
      2m 59s
    7. Working with loops
      8m 1s
    8. Creating functions
      8m 30s
  4. 36m 13s
    1. Working with arrays
      7m 57s
    2. Working with numbers
      6m 13s
    3. Working with strings
      8m 27s
    4. Working with dates
      5m 38s
    5. Working with objects
      7m 58s
  5. 9m 6s
    1. What is the DOM?
      5m 49s
    2. Working with nodes and elements
      3m 17s
  6. 25m 17s
    1. Accessing DOM elements
      11m 3s
    2. Changing DOM elements
      5m 42s
    3. Creating DOM elements
      8m 32s
  7. 24m 45s
    1. Introduction to JavaScript event handling
      8m 16s
    2. Working with onClick and onLoad events
      7m 36s
    3. Working with onBlur and onFocus events
      2m 36s
    4. Working with timers
      6m 17s
  8. 21m 41s
    1. Common JavaScript errors
      7m 14s
    2. Using Firebug
      4m 7s
    3. Going through a debugging session
      10m 20s
  9. 10m 13s
    1. Accessing form elements
      4m 20s
    2. Preventing a form from being submitted
      2m 36s
    3. Hiding and showing form sections
      3m 17s
  10. 9m 49s
    1. CSS and JavaScript
      3m 46s
    2. Removing and applying CSS classes
      2m 16s
    3. Changing inline styles
      3m 47s
  11. 19m 44s
    1. Understanding JavaScript style
      7m 39s
    2. Minifying your code
      4m 28s
    3. Using JavaScript code checkers
      7m 37s
  12. 22m 24s
    1. Introduction to JavaScript libraries
      3m 17s
    2. Linking to multiple JavaScript files
      2m 11s
    3. Introduction to jQuery
      12m 7s
    4. Using a content distribution network to deliver JavaScript files
      4m 49s
  13. 17m 35s
    1. JavaScript in HTML5
      9m 37s
    2. Using Modernizr
      3m 2s
    3. Using Strict Mode
      4m 56s
  14. 33m 3s
    1. Knowing the JavaScript to avoid
      6m 35s
    2. Introduction to regular expressions
      6m 56s
    3. Working with AJAX
      10m 44s
    4. Working with objects and prototypes
      8m 48s
  15. 21m 10s
    1. Example: Countdown
      8m 3s
    2. Example: Resize
      5m 47s
    3. Example: Accordion
      7m 20s
  16. 4m 58s
    1. Where to go from here
      4m 0s
    2. Goodbye
      58s

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

Are you sure you want to delete this note?

No

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.