Skip to Content

Advertisement

You (Level 0)
0% Complete
Last Badge Earned
None

JavaScript date objects are obviously used to track time. You might be thinking, "awesome, I can make a clock." Please don't limit yourself to just building a clock. You can use the date for so much, such as sessions, updates, etc.

When the creators of JavaScript made the date object, they did a wonderful job. In other languages, dates and times are often separated which makes comparison very difficult. However, JavaScript date objects are still quite complicated because the people who created the calendar obviously didn't consider programming.

Date Properties are fairly normal with just the constructor and prototype, neither of which are commonly used.

Common Date Methods

The date object can either get or set times and can compare times.

Getting Times

I am not going to show the examples here, but you should know that all of the date methods that get time return integers. If it was monday getDay() would return 1 instead of Monday (Sunday returns 0).

  • getFullYear() - gets the year (Ex. 2012)
  • getMonth() - gets the month, where January is 0 and December is 11
  • getDate() - gets the day of the month
  • getDay() - gets the day of the week, where Sunday is 0 and Saturday is 6
  • getHours() - gets the hour
  • getMinutes() - gets the minutes
  • getSeconds() - gets the seconds
  • getMilliseconds() - gets the milliseconds, remember that there are 1000 milliseconds in a second
Example <script type="text/javascript">
var d = new Date();
document.write(d.getDay());
</script>
Result
2

Setting Times

Example <script type="text/javascript">
var d = new Date(2012,3,1,20,28,56,445); //year, month, day, hours, minutes, seconds, milliseconds
document.write(d);
</script>
Result
Sun Apr 01 2012 20:28:56 GMT-0400 (Eastern Daylight Time)

While I would say the best way to set a time is when you create it, I understand that there are a few reasons to change a time after it has been set.

  • setFullYear() - sets the year (Ex. 2012)
  • setMonth() - sets the month, where January is 0 and December is 11
  • setDate() - sets the day of the month
  • setDay() - sets the day of the week, where Sunday is 0 and Saturday is 6
  • setHours() - sets the hour
  • setMinutes() - sets the minutes
  • setSeconds() - sets the seconds
  • setMilliseconds() - sets the milliseconds, remember that there are 1000 milliseconds in a second

Comparing Times

Setting and getting times are extremely useful, but what if you want to compare them?

Example <script type="text/javascript">
var oldTime = new Date(2012,3,1,20,28,56,445);
var nowTime = new Date();
if(oldTime.getTime() <= nowTime.getTime())
{
document.write("nowTime is greater than oldTime");
}
else
{
document.write("oldTime is greater than nowTime");
}
</script>
Result
nowTime is greater than oldTime

You have to remember that the nowTime above is a date object. It would not work to say is now time <= some number.

Finally, we have one last important topic to cover with date objects.

  • toString() - converts a date object to a string

The date method toString() is useful when you would like to display only parts of a time in a string. This allows you to join the strings together to show exactly what parts of the time you would like to display. Let's go through an example for a better explanation.

Example <script type="text/javascript">
var nowTime = new Date();
var year = nowTime.getFullYear();
var month = nowTime.getMonth();
var day = nowTime.getDate();
var combined = year + month + " " + day;
document.write(combined);
document.write("<br/>"); //line break
var combinedString = year.toString() + month.toString() + " " + day.toString();
document.write(combinedString);
</script>
Result
2015 3
20123 3

Now you see that, if you don't use the toString(), the year and month are added. However, if we use the toString() methods when we add them we get 20123, which is the year and the month joined not added. I put the day in the example above so you can see that they are not added if a string separates them. So, if you want to use the date objects as strings, use the toString() just to be safe.

I've attached my favorite JavaScript and jQuery book. I would encourage you to read it as well.

Your code will execute in this window.



Let's explore some more tutorials or topics!

Next Tutorial Previous Tutorial

Advertisement

If you enjoyed this resource, support me by sharing this page with others.

Comment on

Show Comments
  • User

    Sophie Cory Nov. 6, 2014, 1:24 p.m.

    Finally know how to do this!



Advertisement