Java Reference
In-Depth Information
and for the third and twenty‐third days, you have this:
case 3:
case 23:
daySuffix = "rd";
break;
Finally, you need the
default
case for everything else. As you will have guessed by now, this is simply
"th"
:
default:
daySuffix = "th";
break;
}
In the final lines you simply write the information to the HTML page, using
document.write()
.
Setting Date Values
To change part of the date in a
Date
object, you have a group of
set
functions, which pretty much
replicate the
get
functions described earlier, except that you are setting, not getting, the values.
These functions are summarized in the following table:
method
desCription
setDate()
The date of the month is passed in as the
parameter to set the date.
setMonth()
The month of the year is passed in as an integer
parameter, where 0 is January, 1 is February, and
so on.
setFullYear()
This sets the year to the four‐digit integer number
passed in as a parameter.
Note
Note that for security reasons, there is no way for web‐based
JavaScript to change the current date and time on a user's computer.
So, to change the year to 2016, the code would be as follows:
myDateObject.setFullYear(2016);
Setting the date and month to the 27th of February looks like this:
myDateObject.setDate(27);
myDateObject.setMonth(1);
One minor point to note here is that there is no direct equivalent of the
getDay()
method. After the
year, date, and month have been defined, the day is automatically set for you.