Connecting Tech Pros Worldwide Help | Site Map

day of month

Prophet
Guest
 
Posts: n/a
#1: Dec 29 '05
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>


RobG
Guest
 
Posts: n/a
#2: Dec 29 '05

re: day of month


Prophet wrote:[color=blue]
> 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.[/color]

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.

[color=blue]
>
> I know this is probably very crude, but this is what I have been using - any
> ideas???
>
>
>
> <script language="JavaScript">[/color]

The language attribute is deprecated, type is required:

<script type="text/javascript">

[color=blue]
> <!--[/color]

HTML comment delimiters inside script elements serve no useful purpose
and are potentially harmful - just don't use them.

[color=blue]
> function tomorrow() {
>
> var mydate=new Date()
> var day=mydate.getDay()
> var daym=mydate.getDate()
> if (daym<10)
> daym="0"+daym
> var tom=day+1[/color]

If tom should be the day number for tomorrow:

var tom = (day + 1)%7;

[color=blue]
> var daymtom=daym+1[/color]

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.


[color=blue]
>
> var week1array=new Array("No Meeting","Surrey","Cloverdale","No
> Meeting","Langley","No Meeting","No Meeting")[/color]

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...

[color=blue]
> 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")}[/color]

Are meetings never held on the 29th, 30th or 31st?

[color=blue]
> }[/color]

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
Jambalaya
Guest
 
Posts: n/a
#3: Dec 29 '05

re: day of month



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

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

Matt Kruse
Guest
 
Posts: n/a
#4: Dec 29 '05

re: day of month


Jambalaya wrote:[color=blue]
> function addZ(n)
> {
> return (n<10) ? '0'+n : ''+n;
> }[/color]

compacting, for amusement:

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

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com


Jambalaya
Guest
 
Posts: n/a
#5: Dec 29 '05

re: day of month



Matt Kruse wrote:[color=blue]
> compacting, for amusement:
>
> function addZ(n){return (n<10&&'0')+n}[/color]

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.

Evertjan.
Guest
 
Posts: n/a
#6: Dec 29 '05

re: day of month


Jambalaya wrote on 29 dec 2005 in comp.lang.javascript:
[color=blue]
>
> Matt Kruse wrote:[color=green]
>> compacting, for amusement:
>>
>> function addZ(n){return (n<10&&'0')+n}[/color]
>
> 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.[/color]

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}


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jambalaya
Guest
 
Posts: n/a
#7: Dec 29 '05

re: day of month


Evertjan. wrote:[color=blue][color=green]
> > function addZ(n){return(n<10&&'0')+''+n}
> >
> > 40 characters and ugly.[/color]
>
> No, thatis wrong:
>
> function addZ(n){return(n<10&&'0')+''+n}
> alert(addZ(17))
>
> // returns: "false17"[/color]

Oops.

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

Jambalaya
Guest
 
Posts: n/a
#8: Dec 29 '05

re: day of month


This should work according to <url:
http://developer.mozilla.org/en/docs...:String:substr[color=blue]
> but IE6 doesn't like it.[/color]

44:
function addZ(n){return('0'+n).substr(-2,2)}

IE treats it like .substr(0,2)

It looks like 40 characters is the lower limit with

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

getting my vote for the best due to its clarity.

Dr John Stockton
Guest
 
Posts: n/a
#9: Dec 29 '05

re: day of month


JRS: In article <FBKsf.8316$km.7369@edtnps89>, dated Thu, 29 Dec 2005
05:36:37 local, seen in news:comp.lang.javascript, Prophet
<M_Moyes@hotmail.com> posted :[color=blue]
>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)[/color]

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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
RobG
Guest
 
Posts: n/a
#10: Dec 30 '05

re: day of month


Evertjan. wrote:[color=blue]
> Jambalaya wrote on 29 dec 2005 in comp.lang.javascript:
>
>[color=green]
>>Matt Kruse wrote:
>>[color=darkred]
>>>compacting, for amusement:
>>>
>>>function addZ(n){return (n<10&&'0')+n}[/color]
>>
>>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.[/color]
>
>
> 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}
>
>[/color]

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! :-)


--
Rob
Prophet
Guest
 
Posts: n/a
#11: Jan 5 '06

re: day of month



"RobG" <rgqld@iinet.net.auau> wrote in message
news:43b3c94d$0$21381$5a62ac22@per-qv1-newsreader-01.iinet.net.au...[color=blue]
> Prophet wrote:[color=green]
>> 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.[/color]
>
> 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.
>
>[color=green]
>>
>> I know this is probably very crude, but this is what I have been using -
>> any ideas???
>>
>>
>>
>> <script language="JavaScript">[/color]
>
> The language attribute is deprecated, type is required:
>
> <script type="text/javascript">
>
>[color=green]
>> <!--[/color]
>
> HTML comment delimiters inside script elements serve no useful purpose and
> are potentially harmful - just don't use them.
>
>[color=green]
>> function tomorrow() {
>>
>> var mydate=new Date()
>> var day=mydate.getDay()
>> var daym=mydate.getDate()
>> if (daym<10)
>> daym="0"+daym
>> var tom=day+1[/color]
>
> If tom should be the day number for tomorrow:
>
> var tom = (day + 1)%7;
>
>[color=green]
>> var daymtom=daym+1[/color]
>
> 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.
>
>
>[color=green]
>>
>> var week1array=new Array("No Meeting","Surrey","Cloverdale","No
>> Meeting","Langley","No Meeting","No Meeting")[/color]
>
> 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...
>
>[color=green]
>> 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")}[/color]
>
> Are meetings never held on the 29th, 30th or 31st?
>
>[color=green]
>> }[/color]
>
> 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[/color]

This worked - thanks!!!!!


Closed Thread