Ken Ward's Java Script Tutorial ...

JavaScript time ...

the months

Hello. Today is:

And the time is now (according to your computer):


  As promised, there is little new to learn to get the months. There is a function getMonth() which which corresponds with getDay(), and as with the days, we need to make an array writing the months in the way we want them displayed.

var myMonths=new Array(

"January","February","March","April","May","June","July","August",

"September","October","November","December");

And as with the days, we can display the month as follows:

var myMonths=new Array(

"January","February","March","April","May","June","July","August",

"September","October","November","December");

today=new Date()

thisMonth=today.getMonth()

document.write(myMonths[thisMonth])

Because this month is then the above code will result in !

However, we might not want this month, and prefer one, say, 3 months on - which is

For example, our next appointment will be in 3 months time, which is .

The code which achieves the above effects is shown below.

var myDate=new Date();
myMonthNum=myDate.getMonth()
myMonthNum=myMonthNum+3
myDate.setMonth(myMonthNum)
myMonth=myDate.getMonth()
document.write(myMonths[myMonth])

  The new thing in the above is setMonth(). We can use this to change the month in any date. We use the numbers 0 to 11 to change the month to another one. In the above code, we extracted the date from the present and added 3 to it. Then we used this new number to set the date to our new month.

Enter a number of months in the box and
click the button to find the new date -
that many months ahead!

Enter months here:

New Date

 

For example, if we enter 78 months, we get:

which is 78 months from today.

 

Ken Ward's HTML Guide...

And for some programming for your brain: check out New Life Course