Re: day of month
- From: RobG <rgqld@xxxxxxxxxxxxxx>
- Date: Thu, 29 Dec 2005 21:32:16 +1000
Prophet wrote:
I have a page which lists of locations of meetings that occur on specific days of the week (every 1st and 3rd Thursday the meeting is in Langley - as an example)
The following code works for when it is the day of the week, but I have a problem when I list tomorrows meeting and today's date is the last day of the week - it will not recognise the 1st day of the next moth as being tomorrows date.
That is because you get today's date, then if it's less than 10 you convert it to a a string, otherwise you keep it as a number.
I know this is probably very crude, but this is what I have been using - any ideas???
<script language="JavaScript">
The language attribute is deprecated, type is required:
<script type="text/javascript">
<!--
HTML comment delimiters inside script elements serve no useful purpose and are potentially harmful - just don't use them.
function tomorrow() {
var mydate=new Date() var day=mydate.getDay() var daym=mydate.getDate() if (daym<10) daym="0"+daym var tom=day+1
If tom should be the day number for tomorrow:
var tom = (day + 1)%7;
var daymtom=daym+1
If daymtom should be tomorrow's date:
mydate.setDate(mydate.getDate() + 1);
var daymtom = mydate.getDate();mydate has now been set to tomorrow, but you don't use it anymore so it should be OK.
You may want to use a function to add the leading zero:
function addZ(n)
{
return (x<10)? '0'+x : ''+x;
}Ensures n is always returned as a string (if that's what you want). Now the line:
var daym=mydate.getDate()
becomes:
var daym = addZ(mydate.getDate());
and the following 'if' statement is redundant.
var week1array=new Array("No Meeting","Surrey","Cloverdale","No Meeting","Langley","No Meeting","No Meeting")
You may find it easier for maintenance to initialise arrays:
var week1array = [
"No Meeting",
"Surrey",
"Cloverdale",
"No Meeting",
"Langley",
"No Meeting",
"No Meeting"
]
Maybe it's generated code so it doesn't matter...
var week2array=new Array("No Meeting","No Meeting","Whiterock","Abbotsford","Hope","No Meeting","No Meeting")
if ((daymtom)<8)
{document.write("<small><font face='Arial'><b>"+week1array[tom]+"</b></font></small>")}
else if ((daymtom)<15)
{document.write("<small><font face='Arial'><b>"+week2array[tom]+"</b></font></small>")}
else if ((daymtom)<22)
{document.write("<small><font face='Arial'><b>"+week1array[tom]+"</b></font></small>")}
else if ((daymtom)<29)
{document.write("<small><font face='Arial'><b>"+week2array[tom]+"</b></font></small>")}
else
{document.write("No Meeting")}
Are meetings never held on the 29th, 30th or 31st?
}
There are a variety of rules to determine which week of the month you are in, make sure users understand what algorithm you are using. Seems to me your week number is given by:
var weekNum = Math.floor((daymtom-1)/7) + 1;
What do you do for dates beyond 28?
[...]
-- Rob .
- Follow-Ups:
- Re: day of month
- From: Jambalaya
- Re: day of month
- References:
- day of month
- From: Prophet
- day of month
- Prev by Date: Re: JSON.parse
- Next by Date: Re: Safari window.location.href
- Previous by thread: day of month
- Next by thread: Re: day of month
- Index(es):
Relevant Pages
|