Ken Ward's Java Script Tutorial ...

JavaScript time ...

days of the week

Hello. Today is:

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

On this page, we will deal with the Months and the Days as we use them in JavaScript.

We can get the day from the date object as follows:

today=new Date()

//as no date has been entered, then it is taken as today.

//today is just a variable and is not a keyword!

thisDay=today.getDay()

document.write(thisDay)

Because today is the result is

This isn't very interesting because no one is going to be impressed by being answered with a number  to 'What's the day today?'.Human beings need to be told the day's name.

To get JavaScript to do this, we need to tell it what this number refers to. And then it can answer with the days's name. So we set up an array of days:

var myDays=

["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]

  In an array, the first element is zero, so "Sunday" is zero ... which ties in with JavaScript very nicely indeed!

So myDays[2], for example, is "Tueday".

The jsEditor can create arrays from a list of elements. This can be a great time saver, and so you can make large arrays easily. You can order the jsEditor online and download the full version immediately, so in a few minutes you could be using it to help you learn and develop scripts. You will also be supporting this site.

We can get today's day as follows:

today=new Date()

thisDay=today.getDay()

thisDay=myDays[thisDay]

document.write(thisDay)

The result is which is today!

So we can use JavaScript's Date() object along with getDay() to find the number, in JavaScript speak, of the day and then use the array of days we have created to get the human name of the day.

This is much easier than teaching people to say, "See you on 3, then". "No, I'm busy on 3, can we make it 5?" When they are busy on Wednesday but can make Friday!

Consider the following example:

Enter a date (use the same format as the example):

Day of week appears here

In this case, we use a Date object with the date 25 December, 2000. (The format is important). In the same way as before, we get the day (getDay()) for this particular date. If you enter another date, then you can find out which day that is. When you click on the button, the following code activates:

<script type="text/javascript">

<!--//

function getTheDay(aText)

{

myDays=

["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]

myDate=new Date(eval('"'+aText.value+'"'))

document.form1.textDay.value=myDays[myDate.getDay()]

}

//-->

</script>

<form name="form1" id="form1" method="post" action="" enctype="text/plain">

<input type="text" name="textDate" id="textDate" value="25 December, 2000" size="25" maxlength="25" />

<input type="button" value="Get day of week" onclick="getTheDay(this.form.textDate)" />

<small>

Day of week appears here

</small>

<br />

<input type="text" name="textDay" VALUE size="25" maxlength="25" />

<br />

</form>

The only thing new here is the use of eval():

myDate=new Date(eval('"'+aText.value+'"'))

We need the date with quotes around it, but, as it appears in the box, it does not have any. So we add the quotes. But this makes a mere string, and we want a Date object. So we use eval() to make the changes.

So, using the Date object in JavaScript, with getDay() we can get the day of the week for any date supported by JavaScript.

Now we know how to extract the days from a date, we will find extracting the Months much easier.

Next: Months


Ken Ward's HTML Guide...

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