• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 12. Objects > The Date Object

The Date Object

The Date object gives you an easy way to store specific dates, ascertain the current date (and time), and find out details about any date (such as its day of the week). For example, I know that I was born on a Wednesday—not because I remember, but because I can check it with the Date object. Basically, I created a new instance of the Date object with my birthday as the initial value, and then I used a method that returns the day of the week. Another interesting application is to repeatedly reassign a variable a new Date object (and use the current date and time for the initial value). Then you can display all the details of the current time (using a clock or calendar). It’s even possible to accurately find the difference (in number of days) between two dates, and you don’t need to know which are leap years or how many days any particular month “hath.” (You know, “Thirty days hath September….”)

Instantiating a Date

Similar to the Color and Sound objects, you always start by instantiating the Date object, and then you can use methods on it. The variable you use to hold a Date object contains a snapshot of a moment in time. That is, a variable that contains the Date data type is holding only one moment in time. When you create an instance of the Date object, you can optionally specify that moment (year, month, hour, second, and even millisecond, if you want); or, if you don’t specify any date, you’re given a date that matches the setting of your user’s computer clock. Here’s the form to create an instance with the current time:


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint