473,383 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

day of month

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>
Dec 29 '05 #1
10 1726
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
Dec 29 '05 #2

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;
}

Dec 29 '05 #3
Jambalaya wrote:
function addZ(n)
{
return (n<10) ? '0'+n : ''+n;
}


compacting, for amusement:

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

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Dec 29 '05 #4

Matt Kruse wrote:
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.

Dec 29 '05 #5
Jambalaya wrote on 29 dec 2005 in comp.lang.javascript:

Matt Kruse wrote:
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.


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)
Dec 29 '05 #6
Evertjan. wrote:
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"


Oops.

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

Dec 29 '05 #7
This should work according to <url:
http://developer.mozilla.org/en/docs...:String:substr
but IE6 doesn't like it.


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.

Dec 29 '05 #8
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_*****@hotmail.com> posted :
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.

--
© 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.
Dec 29 '05 #9
Evertjan. wrote:
Jambalaya wrote on 29 dec 2005 in comp.lang.javascript:

Matt Kruse wrote:
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.

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! :-)
--
Rob
Dec 30 '05 #10

"RobG" <rg***@iinet.net.auau> wrote in message
news:43***********************@per-qv1-newsreader-01.iinet.net.au...
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


This worked - thanks!!!!!
Jan 5 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Finlay | last post by:
Hi Group I am designing a report that should show a meter reading for each month and the previous meter reading for the previous month. The months are text stored in a field tMonth. The...
6
by: Hasanain F. Esmail | last post by:
Hi all, I sincerly thank you all in advance for your help to solve this problem. I have been trying to find a solution to this problem for sometime now but have failed. I am working on a...
6
by: Tony Miller | last post by:
All I have an aggregate query using the function Month & Year on a datereceived field ie: TheYear: Year() TheMonth: Month() These are the group by fields to give me a Count on another field by...
4
by: Ronald Celis | last post by:
Hi, is there anyway to get the Number of days in a given month and Year in C# thanks Ronald Celis
6
by: Ante Perkovic | last post by:
Hi, How to declare datetime object and set it to my birthday, first or last day of this month or any other date. I can't find any examples in VS.NET help! BTW, what is the difference...
0
by: larry | last post by:
I am in the process of rewriting one of my first PHP scripts, an event calendar, and wanted to share the code that is the core of the new calendar. My current/previous calendar processed data...
22
by: Stan | last post by:
I am working with Access 2003 on a computer running XP. I am new at using Access. I have a Db with a date field stored as mm/dd/yyyy. I need a Query that will prompt for the month, ie. 6 for...
19
by: edfialk | last post by:
Hi, does anyone happen to know of a script that would return the number of seconds in a month if I give it a month and a year? My python is a little weak, but if anyone could offer some...
6
by: Nkhosinathie | last post by:
hello guys,i've started with classes and i've been given this program below to program and also i will post the source code if you need it.this program reads as folllows. create a class called...
0
by: marlberg | last post by:
Platform: Windows2000, WindowsXP, Windows Vista, etc Language: C#, ASP.NET Pre-compiled Libraries: Enterprise Library 3.0 full I have a requirement to implement in and display in C# and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.