...

View Full Version : I need help with date object model



StephanieKay90
03-24-2012, 11:08 PM
Hey guys, this is an assignment for school. I have most of the code done but I cant figure out how to completely finish one part of the assignment. I bolded the part in which i'm having trouble with.

Heres the Directions per the Instructor:

Create a new HTML document that creates a listing of the days and date for the current month.

1. Create a new array, named monthArray that holds the names of the all of the months in a year, i.e. January, February…

2. Create a new array, named weekDaysArray that holds all of the days of the week, i.e. Sun, Mon, Tue,….

3. Get today’s date by using the Date object, without initializing a value for the object and save it in a variable named todaysDate.
a. Using the Date object methods, break up the value of todaysDate into variables that hold each of the pieces of the date, by assigning the values to the following variables: (see page 169 in text)
i. currYr = year
ii. currMon = month as an integer
iii. currDate = day of the month
iv. currDay = the day of the week as an integer

4. Use a document.write statement display the name of the month followed by the 4 digit year. (i.e. October 2011)
a. Use currMon as the index to get the name of the month from the monthArray created in step 1.
b. Use the variable currYr created in step 3.
c. Include a blank line after this displayed line

5. Determine the name of the first day of the current month.
a. Using the correct Date Object method, get the integer for the day of the week for the first day of the current month. (hint: you will need to pass the year, month and the day value of 1 to the Date Object to get this number)

6. Use a document.write statement to display every day of the current month on a new line in the format of the name of the day followed by the integer for the date.
a. Use a while loop to keep track of the number of days written for the month.
b. Use a for loop to write out the days of the week from the array created in step 2 under the name of the current month and year.

Example output:
October 2011
Sat 1
Sun 2
Mon 3, etc….until all 31 days are displayed.

My Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Assignment 9</title>

<script type="text/javascript">




var monthArray = new Array ("January","February","March","April","May","June","July","August","September","October","November","December");

var weekDaysArray = new Array ("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");

var todaysDate = new Date();
var currYr = todaysDate.getFullYear();
var currMon = todaysDate.getMonth();
var currDate = todaysDate.getDate();
var currDay = todaysDate.getDay();

document.write(monthArray[currMon] + " " +currYr);
document.write("<br />");
document.write(weekDaysArray[currDay] + "<br />");


var count = 0;
while (count < 31)
{
count++;
}

for(var count = 0 ; count < 31 ; count++)
{
document.write(weekDaysArray[count]+ "<br />");
}


</script>

</head>

<body>

</body>

</html>

devnull69
03-25-2012, 09:49 AM
As this is homework I can only give you some hints

1. Get the current date
2. Invoke the methods to get the date parts for current month and current year (e.g. currentDate.getMonth())
3. Invoke the "new Date" constructor with three parameters (currentYear, currentMonth, 1) to get the first day of the month
4. Invoke the method to get the day of the week for the first day of the month (firstDay.getDay())

StephanieKay90
03-26-2012, 02:18 AM
I don't know how to do #3:

Invoke the "new Date" constructor with three parameters (currentYear, currentMonth, 1) to get the first day of the month

Where do I put this and how?

Lerura
03-26-2012, 03:07 AM
http://www.w3schools.com/jsref/jsref_obj_date.asp

StephanieKay90
03-26-2012, 05:35 AM
i looked at the w3schools and other sites for help as well as my book, but since this is such a specific scenario with how she wants all the dates to appear, unfortunately those do not help me.

StephanieKay90
03-26-2012, 05:39 AM
guys, i have most of the code done, take a look at it! I just need to know where my errors lie (with listing the dates) and how to do the following:

5. Determine the name of the first day of the current month.
a. Using the correct Date Object method, get the integer for the day of the week for the first day of the current month. (hint: you will need to pass the year, month and the day value of 1 to the Date Object to get this number)

6. Use a document.write statement to display every day of the current month on a new line in the format of the name of the day followed by the integer for the date.
a. Use a while loop to keep track of the number of days written for the month.
b. Use a for loop to write out the days of the week from the array created in step 2 under the name of the current month and year.

devnull69
03-26-2012, 08:28 AM
The correct invokation of the Date constructor with year, month and day is


var firstDay = new Date(currentYear, currentMonth, 1); // 1 is the first day of the month of course :-)

Old Pedant
03-26-2012, 10:01 PM
In other words:


var now = new Date()
var firstOfCurrentMonth = new Date( now.getFullYear(), now.getMonth(), 1 );


Or a simpler way:


var now = new Date();
var firstOfCurrentMonth = now.setDate(1);

[But that simpler version doesn't follow what the instructions asked for. Meaning that, as usual, homework problems are not necessarily "real world."]

felgall
03-26-2012, 10:25 PM
[But that simpler version doesn't follow what the instructions asked for. Meaning that, as usual, homework problems are not necessarily "real world."]

There's a lot of things in this question that don't reflect the real world - such as using the completely obsolete document.write statement.

Presumably this is a history course on how JavaScript used to be written back in the 20th Century (the same way the w3schools site provides a hoistorical JavaScript reference from the 20th Century rather than one for how JavaScript should be written now).

This would be a reason why it is almost impossible to provide homework help in JavaScript without having both a copy of the actual question and near complete code for the solution - the question is asking for a 20th Century style script to be written and there are dozens of better ways to write the code now compared to what has been asked for. Without the question and the almost working code any "help" offered would probably use far more modern and efficient processing than has yet been taught in the course (assuming that the course even attempts to teach any real world modern JavaScript) and so would be very obviously NOT written by the person taking the history class.

Old Pedant
03-26-2012, 10:39 PM
T
Presumably this is a history course on how JavaScript used to be written back in the 20th Century.


LMAO! I love it!

Too much truth!

Of course, that means he has already failed the course. Look at the first line:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

felgall
03-26-2012, 11:02 PM
LMAO! I love it!

Too much truth!

Of course, that means he has already failed the course. Look at the first line:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">


document.write iscompletely invalid when you use XHTML and always has been. It was only ever valid with HTML. The script would fail to work at all if you use document.write in a page properly served as XHTML.

JavaScript for XHTML is significantly different in what it allows and what you must use compared to JavaScript for HTML.

So with that doctype the script requested will never work (and in IE8 and earlier the page will be offered for download rather than displaying at all).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum