Home arrow JavaScript arrow Page 2 - 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 Prototype Property
(Page 2 of 5 )

The Prototype property exists in the String Object. And again, it works in the same manner. It allows you to add a property or method to an object.


<html>

<body>


<script type="text/javascript">


function evilemperor(name,title,punishment)

{

this.name=name

this.title=title

this.punishment=punishment

}

var james=new evilemperor("James","Supreme Ruler","Goon Hand")


evilemperor.prototype.salary=null


james.salary="4 Bagillion Dollars!"

document.write("Do not mess up or you will receive the mighty goon hand!")

document.write("<br />")

document.write("The Supreme ruler's salary is:")

document.write("<br />")

document.write(james.salary)


</script>


</body>

</html>

The above code results in:

  Do not mess up or you will receive the mighty goon hand!
  The Supreme ruler's salary is:
  4 Bagillion Dollars!

First it creates variable named evilemperor and stores several values in it. Then it uses prototype to add another data placeholder, and assigns a value to salary. Finally it prints out a bunch of gibberish that I wanted it to say, resulting in the above. Viola!

Next up are the Date Object Methods. But first, here they are in a magnificent table:

Method

What it Does

Date()

Used to retrieve today's date and time

getDate()

Used to retrieve the day of the month from a Date object (from 1-31)

getDay()

Used to retrieve the day of the week from a Date object (from 0-6)

getMonth()

Used to retrieve the month from a Date object (from 0-11)

getFullYear()

Used to retrieve the year, as a four-digit number, from a Date object

getYear()

Used to retrieve the year, as a two-digit or a four-digit number, from a Date object.

getHours()

Used to retrieve the hour of a Date object (from 0-23)

getMinutes()

Used to retrieve the minutes of a Date object (from 0-59)

getSeconds()

Used to retrieve the seconds of a Date object (from 0-59)

getMilliseconds()

Used to retrieve the milliseconds of a Date object (from 0-999)

getTime()

Used to retrieve the number of milliseconds since midnight Jan 1, 1970

getTimezoneOffset()

Gets the difference in minutes between local time and Greenwich Mean Time (GMT)

getUTCDate()

Used to retrieve the day of the month from a Date object according to universal time (from 1-31)

getUTCDay()

Used to retrieve the day of the week from a Date object according to universal time (from 0-6)

getUTCMonth()

Used to retrieve the month from a Date object according to universal time (from 0-11)

getUTCFullYear()

Used to retrieve the four-digit year from a Date object according to universal time

getUTCHours()

Used to retrieve the hour of a Date object according to universal time (from 0-23)

getUTCMinutes()

Used to retrieve the minutes of a Date object according to universal time (from 0-59)

getUTCSeconds()

Used to retrieve the seconds of a Date object according to universal time (from 0-59)

getUTCMilliseconds()

Used to retrieve the milliseconds of a Date object according to universal time (from 0-999)

parse()

Uses a date string to return the number of milliseconds from midnight of January 1, 1970

setDate()

Used to set the day of the month in a Date object (from 1-31)

setMonth()

Used to set the month in a Date object (from 0-11)

setFullYear()

Used to set the year in a Date object (four digits)

setYear()

Used to set the year in the Date object (two or four digits)

setHours()

Used to set the hour in a Date object (from 0-23)

setMinutes()

Used to set the minutes in a Date object (from 0-59)

setSeconds()

Used to set the seconds in a Date object (from 0-59)

setMilliseconds()

Used to set the milliseconds in a Date object (from 0-999)

setTime()

Used to calculate a date and time by adding/subtracting a certain number of milliseconds to or from midnight January 1, 1970

setUTCDate()

Used to set the day of the month in a Date object according to universal time (from 1-31)

setUTCMonth()

Used to set the month in a Date object according to universal time (from 0-11)

setUTCFullYear()

Used to set the year in a Date object according to universal time (a four digit value)

setUTCHours()

Used to set the hour in a Date object according to universal time (from 0-23)

setUTCMinutes()

Used to set the minutes in a Date object according to universal time (from 0-59)

setUTCSeconds()

Used to set the seconds in a Date object according to universal time (from 0-59)

setUTCMilliseconds()

Used to set the milliseconds in a Date object according to universal time (from 0-999)

toSource()

Returns the source code of an object

toString()

Used to convert a Date object to a string

toGMTString()

Used to convert a Date object, according to Greenwich time, to a string

toUTCString()

Used to convert a Date object, according to universal time, to a string

toLocaleString()

Used to convert a Date object, according to local time, to a string

UTC()

Starts with a date and returns the number of milliseconds from midnight of January 1, 1970 according to universal time

valueOf()

Used to return the primitive value of a Date object



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 6 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials