Tuesday, March 19, 2024

The Date( ) Object

Dates and times
present a series of special problems to the computer programmer. In some ways
this is particularly true for a web programmer who has to cater to a global
audience. For example, the time, at any point in time, is at least twenty-four
different times in different parts of the world; all at the same time. Well, you
get the idea!

Some countries present the date as dd/mm/yy while others use mm/dd/yy. Throw in
a Julian date format of yy/ddd and things become even more complicated.

Then there’s the mathematical problem. What was the date twenty-seven days ago?
A while age the British pound was made up of twenty shillings, each of which
comprised twelve pennies. Now, one pound is a hundred (new) pence. One dollar is
a hundred cents — US, Canadian, Hong Kong, Australian, they’re all built the
same way. In fact all major currencies around the world now have this hundred to
one structure (although some of them no longer bother with the fractional
portions) so that simple two decimal place arithmetic is perfectly adequate for
currency transactions.

Inconveniently, however, dates and times are rife with irregularities. Sixty
seconds to a minute, sixty minutes to an hour and then twenty-four hours in a
day. There are, of course, seven days to a week, but how many days are there in
a month? (The number of weeks in a month is, perhaps, an even more interesting
question.) Bear in mind also, that even though all years have twelve months in
them, not all years are the same length. There are not too many people who can
recite the current rules for leap years without looking them up! (There are
exceptions every two hundred and every thousand years, in case you were not
aware!) (Oh, and I say "current" rules because we recently discovered that the
Mayans in Central America may have had a calendar that was more accurate than
our own — we refine our rules periodically to provide accuracy "improvements"!)

With all these time zone issues, formatting problems and mathematical
nightmares, web programmers all over the world would spend hour after hour
writing code to handle the variety of situations their page might have to
contend with unless somebody did something to help them out.

They did.

Included in JavaScript is a Date Object. This object is a part of the overall
specification of JavaScript and is available to any page that can include
JavaScript (these days, that pretty much means any page.) To use it, you create
a new date object instance by setting a variable as follows:

ourDate = new Date( )

"ourDate" can be any variable name you wish. This statement (known as a Date
Constructor) creates an instance of the Date Object, named ourDate. In its raw
form like this, it contains the exact time (as accurately as the local computer
knows it) represented in milliseconds past 00:00 on January first, 1970. It’s
quite a big number! By handling dates and times in this manner, the arithmetic
involved in date calculations is greatly simplified. All you need is an easy way
to convert back and forth — and the Date( ) object includes methods for just
that purpose. Through these methods, the Date( ) object understands the
following date and time fields:

year, month, day, hours, minutes, seconds, milliseconds

The Date( ) object doesn’t have properties that can be directly read and
written. Instead it has methods that are used to work with the data value in the
object. This is a technical difference that stems from the way the date and time
is stored. It’s not possible to read the month directly, for example, it must be
derived from the data by calculation. Calculation requires a method. The Date( )
object includes quite a lot of methods; here’s an alphabetically ordered list:
 

getDate( ) return the day of the month
getDay( ) return the day of the week
getFullYear( ) return the four digit year
getHours( ) return the hours
getMilliseconds( ) return the milliseconds
getMinutes( ) return the minutes
getMonth( ) return the month
getSeconds( ) return the seconds
getTime( ) return the internal representation of the time
getTimezoneOffset( ) return the offset from GMT
getUTCDate( ) return the Universal Time day of the month
getUTCDay( ) return the Universal Time day of the week
getUTCFullYear( ) return the Universal Time four digit year
getUTCHours( ) return the Universal Time hours
getUTCMilliseconds( ) return the Universal Time milliseconds
getUTCMinutes( ) return the Universal Time minutes
getUTCMonth( ) return the Universal Time month
getUTCSeconds( ) return the Universal Time seconds
   
setDate( ) set the day of the month
setDay( ) set the day of the week
setFullYear( ) set the four digit year
setHours( ) set the hours
setMilliseconds( ) set the milliseconds
setMinutes( ) set the minutes
setMonth( ) set the month
setSeconds( ) set the seconds
setTime( ) set the date and time using the millisecond format
setUTCDate( ) set the Universal Time day of the month
setUTCDay( ) set the Universal Time day of the week
setUTCFullYear( ) set the Universal Time four digit year
setUTCHours( ) set the Universal Time hours
setUTCMilliseconds( ) set the Universal Time milliseconds
setUTCMinutes( ) set the Universal Time minutes
setUTCMonth( ) set the Universal Time month
setUTCSeconds( ) set the Universal Time seconds
   
toGMTstring( ) return the date and time as a string, using the GMT time
zone
toLocaleString( ) return the date and time as a string, using the local time
zone
toString( ) return the date and time as a string
toUTCString( return the date and time as a string, using Universal Time
   
valueOf( ) convert a date to the internal millisecond format

Two other methods, getYear( ) and setYear( ), also exist but are
deprecated in favor of the "Full" versions listed above.

The constructor also has two methods that you can invoke. 
These are Date.parse( ) and Date.UTC( )

In the next part of this series, we look at the use of these
methods.

 

[Part Three:  Using The Date( )
Object Methods
]
 

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Popular Articles

Featured