Home arrow JavaScript arrow Page 4 - JavaScript Objects: Dates
JAVASCRIPT

JavaScript Objects: Dates


In the last tutorial we discussed the last of the String Objects. We left off with two to go, but one of those two doesn't work in IE (and therefore is not worth mentioning; VIVE BILL GATES!) and the other is code that JavaScript creates automatically for you. So I guess what I am saying here is I am not going to discuss those. I will however discuss my good buddy, the Date Object.

Author Info:
By: James Payne
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
February 04, 2008
TABLE OF CONTENTS:
  1. · JavaScript Objects: Dates
  2. · The Prototype Property
  3. · The Date() Method
  4. · The GetDate() Method
  5. · The GetMonth() Method

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
JavaScript Objects: Dates - The GetDate() Method
(Page 4 of 5 )

Here is a method you nerds could really use. Don't get your hopes up though; it isn't used to get you that kind of date. It is used to extract the day of the month. Here it is in some perty code:


<html>

<body>


<script type="text/javascript">


var when = new Date()

document.write(when.getDate())


</script>


</body>

</html>

This example creates a new variable named "when." It then uses the GetDate() method to extract today's date from the variable (which we filled with data from the Date() function).

The result is:

  28

The GetDay() Method

The GetDay() method allows us to see what day of the week it is, in numeric form. It is coded like this:


<html>

<body>


<script type="text/javascript">


var when = new Date()

document.write("Today is this day of the week:")

document.write(when.getDay())


</script>


</body>

</html>

This would result in:

Today is this day of the week:

  4

Note that Sunday is the 0 day of the week, so 4 would be Thursday.

Now what if we wanted to create a more helpful program that went ahead and told us the day of the week with a word, so we didn't have to use our brain? Well, we could do it this way:


<html>

<body>


<script type="text/javascript">


var when=new Date()

var whatday=new Array(7)

whatday[0]="Sunday!"

whatday[1]="Monday!"

whatday[2]="Tuesday!"

whatday[3]="Wednesday!"

whatday[4]="Thursday!"

whatday[5]="Friday!"

whatday[6]="Saturday!"


document.write("The day of the week is... " + whatday[when.getDay()])


</script>


</body>

</html>

This would print out:

The day of the week is...Thursday!


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials