PDA

View Full Version : Forgot how to JavaScript


Cuc Tu
07-20-2009, 08:27 PM
I just don't use it enough.

I'm trying to show the current ISO week number on my HTML page.

The page is really simple with just a few paragraphs and a table.

Under body in paragraph tag, I insert:


<script type="text/javascript">document.write(date());</script>


Nothing shows there, so i try some other code I found:



<SCRIPT LANGUAGE="JavaScript1.2">
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Begin
var months=new Array(13);
months[1]="January";
months[2]="February";
months[3]="March";
months[4]="April";
months[5]="May";
months[6]="June";
months[7]="July";
months[8]="August";
months[9]="September";
months[10]="October";
months[11]="November";
months[12]="December";
var time=new Date();
var lmonth=months[time.getMonth() + 1];
var date=time.getDate();
var year=time.getYear();
if (year < 2000)
year = year + 1900;
document.write("<center>" + lmonth + " ");
document.write(date + ", " + year + "</center>");
// End -->
</script>


That shows the date. I still need the week number and found this good looking code:



<script type="text/javascript">
/**
* Get the ISO week date week number
*/
Date.prototype.getWeek = function () {
// Create a copy of this date object
var target = new Date(this.valueOf());

// ISO week date weeks start on monday
// so correct the day number
var dayNr = (this.getDay() + 6) % 7;

// Set the target to the thursday of this week so the
// target date is in the right year
target.setDate(target.getDate() - dayNr + 3);

// ISO 8601 states that week 1 is the week
// with january 4th in it
var jan4 = new Date(target.getFullYear(), 0, 4);

// Number of days between target date and january 4th
var dayDiff = (target - jan4) / 86400000;

// Calculate week number: Week 1 (january 4th) plus the
// number of weeks between target date and january 4th
var weekNr = 1 + Math.ceil(dayDiff / 7);

return weekNr;
}

</script>


But it does not show the week number anywhere, so I add the following line before the end script tag:


document.write("Week" + weekNr);


But it does not write anything, not even the word "Week"

So I start looking at the code that actually wrote the date. It has these elements, which I add:


<!-- Begin
// End -->


But nothing shows.

So now I realize that I don't know what I'm doing.

Also, I tried the code here, but I have the same sort of result, mostly not knowing how to use it?


http://www.meanfreepath.com/support/getting_iso_week.html



I also tried the code in the head with the document.write function in the body.

?

Cuc Tu
07-21-2009, 05:28 PM
OK, some progress. The script returns the wrong week number. It say 53 when it should say 30. Any ideas?



<script type="text/javascript">

document.write("<br /><br />run ISO DOW Week Number");

/**

* Returns the week number for this date. dowOffset is the day of week the week
* "starts" on for your locale - it can be from 0 to 6. If dowOffset is 1 (Monday),
* the week returned is the ISO 8601 week number.
* @param int dowOffset
* @return int
*/

Date.prototype.getWeek = function (dowOffset) {

/*getWeek() was developed by Nick Baicoianu at MeanFreePath: http://www.meanfreepath.com */

dowOffset = typeof(dowOffset) == 'int' ? dowOffset : 0;
//default dowOffset to zero
var newYear = new Date(this.getFullYear(),0,1);
var day = newYear.getDay() - dowOffset;
//the day of week the year begins on
day = (day >= 0 ? day : day + 7);
var daynum = Math.floor((this.getTime() - newYear.getTime() - (this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
var weeknum;
//if the year starts before the middle of a week
if(day < 4) {
weeknum = Math.floor((daynum+day-1)/7) + 1;
if(weeknum > 52) {
nYear = new Date(this.getFullYear() + 1,0,1);
nday = nYear.getDay() - dowOffset;
nday = nday >= 0 ? nday : nday + 7;
//if the next year starts before the middle of the week, it is week #1 of that year//
weeknum = nday < 4 ? 1 : 53;
}
}
else {
weeknum = Math.floor((daynum+day-1)/7);
}
return weeknum;
};

document.write("<br />ISO DOW Number = " + Date.prototype.getWeek(1));

</script>

Cuc Tu
07-22-2009, 01:11 AM
OK, never mind that mess...

I figured since there are two ways to interpret the week number, whether it's ISO or US with day 1 on Sunday or Monday, there is no point...

I'll just call it the week of July 20th, 2009, and just to be abstract it will be presented as 20090720, for good rhyme and reason.

Cuc Tu
07-23-2009, 08:28 PM
Nope, I'm back to the week format...