Forum Moderators: open

Message Too Old, No Replies

Date last Wednesday

         

ETinOz

5:23 am on Jul 7, 2008 (gmt 0)

10+ Year Member



Hi all,

have been trying to put together a script to write the date for last Wednesday, from the present.

The script below works, but not when it goes back to the previous month.

All suggestions appreciated.
Thanks in advance,
ET

<script type="text/javascript">
<!--
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDay()
var year = currentTime.getFullYear()
if(day == '0'){
var date = currentTime.getDate() - 4
}else if(day == '1'){
var date = currentTime.getDate() - 5
}else if(day == '2'){
var date = currentTime.getDate() - 6
}else if(day == '4'){
var date = currentTime.getDate() - 1
}else if(day == '5'){
var date = currentTime.getDate() - 2
}else if(day == '6'){
var date = currentTime.getDate() - 3
} else {
var date = currentTime.getDate()
}
document.write(date + "/" + month + "/" + year)
//-->
</script>

Fotiman

2:39 pm on Jul 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Note, don't put HTML comments inside your script tag. They are not needed.


<script type="text/javascript">
var currentTime = new Date();
var currentDay = currentTime.getDay();
var dayOfWeek = 3; // 0 = Sun, 3 = Wed, 6 = Sat
var daysBack, pastDate;
if (currentDay > dayOfWeek) {
daysBack = currentDay - dayOfWeek;
}
else {
daysBack = (7 - dayOfWeek) + currentDay;
}
// Convert days back to milliseconds
// days * num hours in a day * num mins in an hour * num sec in a min * num ms in a sec
daysBack = daysBack * 24 * 60 * 60 * 1000;
pastDate = new Date(currentTime.getTime() - daysBack);
document.write(pastDate.getDate() + "/" + (pastDate.getMonth() + 1) + "/" + pastDate.getYear());
</script>

ETinOz

3:44 am on Jul 8, 2008 (gmt 0)

10+ Year Member



thanks for that,
your code works fine, but gave the current year as 108 rather than 08.
My 11 year old son then modified the code by adding a -100 to the getYear and it now gives 08 as the year. Its good to have children around , lol.
Thanks again,
regards
ET

ETinOz

5:40 am on Jul 8, 2008 (gmt 0)

10+ Year Member



my apologies...

your script works brilliantly with IE7, it doesnt come out the same in FF.

The amendment we made makes it work in FF.

thanks again

ET

g1smd

10:15 am on Jul 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Never use a two digit year. Always use all four digits.

It aids user readability, and it prevents "century rollover" issues.

Fotiman

1:33 pm on Jul 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



My bad. Here is the updated script which uses getFullYear() instead of getYear().


<script type="text/javascript">
var currentTime = new Date();
var currentDay = currentTime.getDay();
var dayOfWeek = 3; // 0 = Sun, 3 = Wed, 6 = Sat
var daysBack, pastDate;
if (currentDay > dayOfWeek) {
daysBack = currentDay - dayOfWeek;
}
else {
daysBack = (7 - dayOfWeek) + currentDay;
}
// Convert days back to milliseconds
// days * num hours in a day * num mins in an hour * num sec in a min * num ms in a sec
daysBack = daysBack * 24 * 60 * 60 * 1000;
pastDate = new Date(currentTime.getTime() - daysBack);
document.write(pastDate.getDate() + "/" + (pastDate.getMonth() + 1) + "/" + pastDate.getFullYear());
</script>