test for current monday

1. bnk
Member
Join Date
Dec 1969
Posts
35

test for current monday

Anyone know how to use Javsacript to test for the current monday? So for example, if I were to run the test today, it would show 12/09/2003 as the current monday. I then have to write it on a page. Any help would be very helpful. I don&#039;t have a clue. Thanks again!

2. Senior Member
Join Date
Dec 1969
Posts
353

RE: test for current monday

Well, Monday registers as 1 in the .getDay() method of the Date() object. All you need to do is subtract 1 from the value of .getDay() to find the number of days ahead of the previous Monday. Although the problem arises because Sunday = 0. So, you need to replace 0 with 7.<BR><BR>function today(oTheDate) // parameter oTheDate, a Date object<BR>{<BR>iDay = oTheDate.getDay();<BR><BR>if (iDay == 0)<BR>iDay = 7;<BR><BR>return iDay;<BR>}<BR><BR>So now you have the number of days that you have to subtract from the current date. The best way I can think of to do this is as follows.<BR><BR>function getMonday()<BR>{<BR>oDate = new Date()<BR><BR>iDaysSinceMonday = today();<BR><BR>iMillisInADay = 86400000; // Milliseconds in a day. 1000 * 60 * 60 * 24<BR><BR>iMillisSinceMonday = iDaysSinceMonday * iMillisecondsInADay;<BR><BR>oMondaysDate = new Date(iMilliseconds - iMillisSinceMonday);<BR><BR>return oMondaysDate;<BR>}<BR><BR>Now all you have to do is pump this through your favourite date formatting function.

3. Senior Member
Join Date
Dec 1969
Posts
96,118

Not too hard...

&#060;HTML&#062;&#060;BODY&#062;<BR><BR>&#060;SCRI PT Language=JavaScript&#062;<BR>var now = new Date();<BR>var curWeekDay = now.getDay( );<BR>alert(curWeekDay);<BR>now.setTime( now.getTime() - 24 * 3600 * 1000 * (curWeekDay-1) );<BR>document.write( now.toString( ) );<BR>&#060;/SCRIPT&#062;<BR><BR>&#060;/BODY&#062;&#060;/HTML&#062;<BR><BR>********************<BR><BR>That will convert Sunday, 8 Dec 2002 to Monday, 9 Dec 2002, instead of to the prior Monday.<BR><BR>If you need for Sunday to go back to the prior Monday, it&#039;s just a minor tweak.<BR><BR>

4. Senior Member
Join Date
Dec 1969
Posts
96,118

You never define the variable "iMilliseconds" that you use in next to last code line there.<BR><BR>Also, your function "today()" is expecting an argument. But you don&#039;t pass one when you call it.<BR><BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
96,118

Oops...and another fix...

...you forgot to subtract 1 from iDaysSinceMonday.<BR><BR>

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•