You can check out my prac here:
http://tracyr.comlu.com/script/blog.html
It wasn't too difficult. Looking at the resources Alistair posted in UTS Online, I opened up the date file, and it suggested that to find a difference between two dates, do this:
timeA = new Date();
timeB = new Date( 'June 8 1982' );
timeDifference = timeA - timeB;
If 'new Date' is left blank, then it will assume today's date. The second date is my birthday- so this will calculate the difference between today's date and my birthday.
The only problem with this is that it's in milliseconds. In the same reference article for javascript date, it mentions there are 86 400 000 milliseconds in a day, so to get the number of days is pretty simple- take the time difference (which is now in milliseconds) and divide it by 86 400 000- which will give the number of days. To diplay this number in the document, I added the 'document.write'.
result= timeDifference/86400000;
document.write(result);
The last thing I did (mainly to check my maths since I wasn't convinced I was doing the right calculations!) was to divide the number of days I came up with by 365. This then converts the days into years. Now it's only approximate because it doesn't take into consideration leap years, but it was accurate enough to check by maths since I know how old I am!
To do this I added:
years = result/365;
And the final thing I did was to write this number to the document, but I wanted to write it so it was a whole number, without the decimal points, so I added in 'math.round' which rounds it to the nearest whole number- taken straight from w3schools.
document.write(Math.round(years) + "
");
So that was the JavaScript Prac!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment