day of month

P

Prophet

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.

I know this is probably very crude, but this is what I have been using - any
ideas???



<script language="JavaScript">
<!--
function tomorrow() {

var mydate=new Date()
var day=mydate.getDay()
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
var tom=day+1
var daymtom=daym+1

var week1array=new Array("No Meeting","Surrey","Cloverdale","No
Meeting","Langley","No Meeting","No Meeting")
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")}
}
// -->
</script>
 
R

RobG

Prophet said:
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:


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?


[...]
 
J

Jambalaya

RobG wrote:
[snip]
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).

function addZ(n)
{
return (n<10) ? '0'+n : ''+n;
}
 
J

Jambalaya

Matt said:
compacting, for amusement:

function addZ(n){return (n<10&&'0')+n}

37 characters (if you remove the space after "return"), but it doesn't
stringify n >= 10 which was one of the secondary goals, I believe. How
about

function addZ(n){return(n<10&&'0')+''+n}

40 characters and ugly.
 
E

Evertjan.

Jambalaya wrote on 29 dec 2005 in comp.lang.javascript:
37 characters (if you remove the space after "return"), but it doesn't
stringify n >= 10 which was one of the secondary goals, I believe. How
about

function addZ(n){return(n<10&&'0')+''+n}

40 characters and ugly.

No, thatis wrong:

function addZ(n){return(n<10&&'0')+''+n}
alert(addZ(17))

// returns: "false17"

==================================

The original, 41:

function addZ(n){return(n<10)?'0'+n:''+n}

40:
function addZ(n){return(n>9)?''+n:'0'+n}

40:
function addZ(n){return((n>9)?'':'0')+n}

48:
function addZ(n){return((100+n)+'').substr(1,2)}

50:
function addZ(n){return(''+n/10).substr(0,1)+n%10}
 
J

Jambalaya

Evertjan. said:
No, thatis wrong:

function addZ(n){return(n<10&&'0')+''+n}
alert(addZ(17))

// returns: "false17"

Oops.

40:
function addZ(n){return(n<10&&'0')+n+''}
 
D

Dr John Stockton

JRS: In article <FBKsf.8316$km.7369@edtnps89>, dated Thu, 29 Dec 2005
05:36:37 local, seen in Prophet
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)

You'll need to think a bit more, perhaps, if a meeting is on the last
Tuesday of each month.

See <URL:http://www.merlyn.demon.co.uk/js-dates.htm>.

Be aware that javascript numbering of the days of the week does not
agree with ISO 8601.

See also article "Javascript Date bugs", posted early in most years.
 
R

RobG

Evertjan. said:
Jambalaya wrote on 29 dec 2005 in comp.lang.javascript:




No, thatis wrong:

function addZ(n){return(n<10&&'0')+''+n}
alert(addZ(17))

// returns: "false17"

==================================

The original, 41:

function addZ(n){return(n<10)?'0'+n:''+n}

40:
function addZ(n){return(n>9)?''+n:'0'+n}

40:
function addZ(n){return((n>9)?'':'0')+n}

48:
function addZ(n){return((100+n)+'').substr(1,2)}

50:
function addZ(n){return(''+n/10).substr(0,1)+n%10}

It should probably be noted that the number of characters is not a
reliable indication of the efficiency or speed of the function.

Code that is optimised for speed can often be written more concisely,
but speed may well be adversely affected and legibility is nearly
always the loser.

But hey, have fun! :)
 
P

Prophet

RobG said:
Prophet said:
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:


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?


[...]

This worked - thanks!!!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top