...

View Full Version : Display Certain Messages depending on Date Range



CarabinerJ
01-26-2012, 05:04 PM
I'm wanting to put a piece of code on my website that will display when our next meeting will be. We have 2 meetings per month. I'm thinking what I need to do is create a code that gets the current month, day of month, and year then combines it together somehow, and if that combination is in a certain range a particular statement, "Next meeting is MM, DD YY." or "Next meeting is today." is displayed.

If date is equal to or between 01012012 and 01112012, "Next meeting is January, 12 2012" is displayed.
If date is equal to 01122012, "Next meeting is today" is displayed.
If date is equal to or between 01132012 and 01252012, "Next meeting is January, 26 2012" is displayed.
If date is equal to 01262012, "Next meeting is today" is displayed.
...that kind of thing.

How would I combine the month, day of month, and year?
Would 48 if statements be what I need to do? :confused:

Sorry, I'm pretty new to javascript. If there's a better way to do this, please let me know.

Thanks,

CJ

xelawho
01-26-2012, 06:51 PM
are the meetings always on the same day of the month (ie, the 12th and the 26th) - if so it could be like this:



<html>
<head>
</head>
<body>

<div id="date"></div>
<script type="text/javascript">
var months=["January", "February", "March", "April", "May", "June", "July", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
var d=new Date()
var day=d.getDate()
var year=d.getFullYear()
if(day>=1&&day<=11){
text="Next meeting is "+months[d.getMonth()]+ ", 12 "+year
} else if (day>=13&&day<=25){
text="Next meeting is "+months[d.getMonth()]+ ", 26 "+year
} else if (day==12||day==26){
text="Meeting is today"
} else if (day>=27){
if(d.getMonth()==11){
text="Next meeting is January 12, "+(year+1)
} else {
text="Next meeting is "+months[d.getMonth()+1]+ ", 12 "+year
}
}


document.getElementById("date").innerHTML=text
</script>
</body>
</html>

Philip M
01-26-2012, 07:02 PM
I think the meetings are every 14 days, on alternate Thursdays but perhaps no meeting on certain days such as Christmas Day?? He mentions 48 statements.


<html>

<head>
</head>

<body>
<span id ="nextMeeting">Next meeting is today</span>

<script type = "text/javascript">

var today = new Date();

today = new Date("January 25, 2012"); // for testing

var eventDate = new Date("January 12, 2012");
var ed = new Date("January 12, 2012");
ed.setDate(ed.getDate() - 13); // 13 days in the past 30th December
if ((today < eventDate) && (today >= ed)) {document.getElementById("nextMeeting").innerHTML = "The next meeting is January 12 2012"}

var eventDate = new Date("January 26, 2012");
var ed = new Date("January 26, 2012");
ed.setDate(ed.getDate() - 13); // 13 days in the past
if ((today < eventDate) && (today >= ed)) {document.getElementById("nextMeeting").innerHTML = "The next meeting is January 26 2012"}

var eventDate = new Date("February 9, 2012");
var ed = new Date("February 9, 2012");
ed.setDate(ed.getDate() - 13); // 13 days in the past
if ((today < eventDate) && (today >= ed)) {document.getElementById("nextMeeting").innerHTML = "The next meeting is February 9 2012"}

// and so on

</script>


</body>

</html>

CarabinerJ - Do please read the posting guidelines regarding silly thread titles. The thread title is supposed to help people who have a similar problem in future.
Yours is useless for this purpose. You can (and should) edit it to make it more meaningful.




"By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest." - Confucious

CarabinerJ
01-26-2012, 08:34 PM
Sorry! Edited the thread title.

The meetings are always the 2nd and 4th Thursdays of the month. Except November's meetings this year are the 2nd and 5th Thursdays.

1/12, 1/26
2/9, 2/23
3/8, 3/22
4/12, 4/26
5/10, 5/24
6/14, 6/28
7/12, 7/26
8/9, 8/23
9/13, 9/27
10/11, 10/25
11/8, 11/29
12/13, 12/27

Philip M
01-27-2012, 08:21 AM
In that case you should use the script I gave you in Post #3.

You can update it (say) once a month replacing the 2012 dates for the month now past by the 2013 dates for that same month.

xelawho
01-27-2012, 12:58 PM
... or just put your meting dates into sub-arrays...



<html>
<head>
</head>
<body>

<div id="date"></div>
<script type="text/javascript">
var months=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
var meets=[[12, 26],[9,23],[8,22],[12,26],[10,24],[14,28],[12,26],[9,23],[13,27],[11,25],[8,29],[13,27]]
var d=new Date()
var day=d.getDate()
var year=d.getFullYear()
var month=d.getMonth()
if(day<meets[month][0]){
text="Next meeting is "+months[month]+ " "+meets[month][0]+ " "+year
} else if (day>meets[month][0]&&day<meets[month][1]){
text="Next meeting is "+months[month]+ " "+meets[month][1]+ " "+year
} else if (day==meets[month][0]||day==meets[month][1]){
text="Meeting is today"
} else if (day>meets[month][1]){
if(d.getMonth()==11){
text="Next meeting is not yet scheduled"
} else {
text="Next meeting is "+months[month+1]+ " "+meets[month+1][0]+ " "+year
}
}

document.getElementById("date").innerHTML=text
</script>
</body>
</html>

Philip M
01-27-2012, 04:30 PM
A lot simpler than mine! :o Small correction:-

else if (day>meets[month][1]) {
if (month==11) {
text="Next meeting is not yet scheduled"
}

xelawho
01-27-2012, 04:55 PM
:thumbsup:

Philip M
01-28-2012, 09:05 AM
For the benefit of future visitors to this thread, I have modified xelawho's excellent script to allow for the situation where in a given month no meetings, or only one meeting, are scheduled. It does not work if there are no meeetings scheduled in two consecutive months!


<html>
<head>
</head>
<body>

<div id="date"></div>

<script type="text/javascript">

// if there is a month with no meeting enter dates as 0,0
// if only one meeting in the month enter as 25,25

var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var meets = [[12,26],[0,0],[22,22],[12,26],[10,24],[14,28],[12,26],[9,23],[13,27],[11,25],[8,29],[13,27]];
var d=new Date();
var day=d.getDate();
var year=d.getFullYear();
var month=d.getMonth();
var m1 = month+1;

if (day<meets[month][0]) {
text="Next meeting is "+months[month]+ " "+meets[month][0]+ " "+year;
}
else if (day>meets[month][0]&&day<meets[month][1]) {
text="Next meeting is "+months[month]+ " "+meets[month][1]+ " "+year;
}
else if (day==meets[month][0]||day==meets[month][1]) {
text="Meeting is today"
}
else if (day>meets[month][1]) {
if (month==11) { // December
text="Next meeting is "+months[0]+ " "+meets[0][0]+ " "+ (year+1);
}
else {
if (m1 ==12) {m1=0}
if ( (meets[m1][0]==0) && (meets[m1][1]==0) ) { // no meeting that month
m1 = m1 +1; // add a month
if (m1 ==12) {m1=0; year = year +1}
}
text="Next meeting is "+months[m1]+ " "+meets[m1][0]+ " "+year;
}
}

document.getElementById("date").innerHTML=text;

</script>
</body>
</html>

xelawho
01-28-2012, 03:30 PM
... and so in complicating things, we arrive at a (somewhat) simpler solution. This appears to accept multiple meetings per month and any number of consecutive months without scheduled meetings...



<html>
<head>
</head>
<body>
<div id="date"></div>
<script type="text/javascript">
var months=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
var meets = [[12,26],[0],[0],[0],[12,26,28],[14,28],[12,26],[9,23],[13,27],[11,25],[8,29],[13,27]];
var d=new Date()
var day=d.getDate()
var year=d.getFullYear()
var month=d.getMonth()

for ( x = 0; x < meets.length; x++ ){
for ( a = 0; a<meets[x].length; a++ ){
if(month==x){
if (day>meets[month][meets[month].length-1]||meets[x][a]==0){
month+=1
day=meets[month][0]-1
}
else if(day==meets[x][a]){
text="Meeting is today"
break;
}
else if (day<meets[x][a]){
text="Next meeting is "+months[x]+ " "+meets[x][a]+ " "+year
break;
}
}
}
}
document.getElementById("date").innerHTML=text
</script>
</body>
</html>

Philip M
01-28-2012, 04:16 PM
Next meeting is May 12 2012 :(

var meets = [[12,26], [0,0,0] ,[12,26,28],[14,28],[12,26],[9,23],[13,27],[11,25],[8,29],[13,27]];

Breaks on month = 11. text is not defined.

xelawho
01-28-2012, 05:25 PM
oops - forgot about month=11. have added that if. please also note that the meets array is not
var meets = [[12,26], [0,0,0] etc
but var meets = [[12,26],[0],[0],[0] etc
(you only need to put one zero in a month to denote no meeting, and months with one meeting can just have that number - see august)



<html>
<head>
</head>
<body>
<div id="date"></div>
<script type="text/javascript">
var months=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
var meets = [[12,26],[0],[0],[0],[12,26,28],[14,28],[12,26],[25],[13,27],[11,25],[8,29],[13,27]];
var d=new Date()
var day=d.getDate()
var year=d.getFullYear()
var month=d.getMonth()

for ( x = 0; x < meets.length; x++ ){
for ( a = 0; a<meets[x].length; a++ ){
if(month==x){
if (day>meets[month][meets[month].length-1]||meets[x][a]==0){
if(month==11){text="Next meeting is not yet scheduled"
break;
}
month+=1
day=meets[month][0]-1
}
else if(day==meets[x][a]){
text="Meeting is today"
break;
}
else if (day<meets[x][a]){
text="Next meeting is "+months[x]+ " "+meets[x][a]+ " "+year
break;
}
}
}
}
document.getElementById("date").innerHTML=text
</script>
</body>
</html>

Philip M
01-28-2012, 06:28 PM
No, still says Next meeting is May 12 2012 caused by [0],[0],[0].

var meets = [[12,26], [0,0,0] ,[12,26,28],[14,28],[12,26],[25],[13,27],[11,25],[8,29],[13,27]];

Still breaks on month = 11;

Perhaps you have posted the wrong (uncorrected) version?

xelawho
01-28-2012, 06:38 PM
no - that [0],[0],[0] is to demonstrate what happens if there are no meetings in Feb, Mar or Apr, so "next meeting May 12" is a correct result, being that January's last meeting was the 26th and today is the 28th.

of coursevar meets = [[12,26], [0,0,0] ,[12,26,28],[14,28],[12,26],[25],[13,27],[11,25],[8,29],[13,27]]; breaks on 11 - there's only 10 elements in the array!

Philip M
01-28-2012, 06:47 PM
no - that [0],[0],[0] is to demonstrate what happens if there are no meetings in Feb, Mar or Apr, so "next meeting May 12" is a correct result, being that January's last meeting was the 26th and today is the 28th.

of coursevar meets = [[12,26], [0,0,0] ,[12,26,28],[14,28],[12,26],[25],[13,27],[11,25],[8,29],[13,27]]; breaks on 11 - there's only 10 elements in the array!

Oh, sorry! You are entirely right! I was wrongly expecting a meeting on March 12th. :o

xelawho
01-28-2012, 06:49 PM
no worries, Philip - I appreciate your critical eye.

one question, though: did I just teach my grandmother how to suck an egg? ;)

Philip M
01-28-2012, 06:56 PM
no worries, Philip - I appreciate your critical eye.

one question, though: did I just teach my grandmother how to suck an egg? ;)

Not really. I guess my mind was just working on different lines. This was an interesting problem. Your code is certainly good stuff, but two heads can sometimes be better than one!

There is still a small problem - if the current date is December 28th - 31st the message is "Next meeting is not yet scheduled" rather than the date of the first meeting in January of the following year (here 12th but presumably updated by that time).

A solution is


if (month == 11) {
if (meets[0][0] == 0) {text="Next meeting is not yet scheduled"}
else {text = "Next meeting is on " + months[0] + " " + meets[0][0] + " " + (year+1)}
break;
}

xelawho
01-29-2012, 02:18 PM
... or (in keeping with the original spirit of the script and allowing for the possibility that there really is no meeting scheduled for Jan 2013)...



<html>
<head>
</head>
<body onload="getMeet()">
<div id="date"></div>
<script type="text/javascript">
var months=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
var meets = [[0],[0],[25],[0],[12,26,28],[14,28],[12,26],[25],[13,27],[11,25],[8,29],[13,27]];
var d=new Date("December 29, 2012")
var day=d.getDate()
var year=d.getFullYear()
var month=d.getMonth()

function getMeet(){
for ( x = 0; x < meets.length; x++ ){
for ( a = 0; a<meets[x].length; a++ ){
if(month==x){
if (day>meets[x][meets[x].length-1]||meets[x][a]==0){
if(month==11){
december();
break;
}
month+=1
day=0
}
else if(day==meets[x][a]){
text="Meeting is today"
break;
}
else if (day<meets[x][a]){
text="Next meeting is "+months[x]+ " "+meets[x][a]+ " "+year
break;
}
}
}
}
document.getElementById("date").innerHTML=text
}

function december () {
month=0
day=0
year+=1
getMeet()
}

</script>
</body>
</html>

Philip M
01-29-2012, 03:41 PM
I think that has finally cracked it! :)

Philip M
01-30-2012, 01:16 PM
Another small improvement - the message "Meeting is today" will display up to 2359hrs on the meeting day, even though the meeting is presumably over by then. So-


var d=new Date();
if (d.getHours()>=18) {d.setDate(d.getDate() + 1)} // increment date after 1800 hours or time when meeting is over

Snag! This will show "Next meeting is today" after 1800 on the day before the meeting!!

So simpler solution:-


else if (day==meets[x][a]) {
text="Our next meeting is today at 14:30 in the Club House"; // better put in the time as message will show after the meeting is over
break;

CarabinerJ
01-30-2012, 03:08 PM
You guys are awesome! Thank you for your hard work!

xelawho
01-30-2012, 03:17 PM
ah, Philip. you give up way too easily. whatever happened to:


else if(day==meets[x][a]){
if (d.getHours()>=18) {
day+=1
getMeet();
break;
}

Philip M
01-30-2012, 03:28 PM
Yes, I thought of that, but day+1 will not work! Surely you must use d.setDate(d.getDate() + 1); day = d.getDate();

I finished up with:-


<html>
<head>
</head>

<body onload="getMeet()">

<div id="date"></div>

<script type="text/javascript">

//This accepts multiple meetings per month and any number of consecutive months without scheduled meetings...

var months=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
var meets = [[12,26],[0],[25],[0],[12,26,28],[14,28],[12,26],[25],[13,27],[11,25],[8,29],[13,27]];
var d=new Date();
var day = d.getDate();
var hrs = d.getHours();
var year = d.getFullYear();
var month = d.getMonth();

day = 26; // for testing
month = 0; // for testing
hrs = 12; // for testing

function getMeet() {
for ( x = 0; x < meets.length; x++ ) {
for ( a = 0; a<meets[x].length; a++ ) {
if (month==x) {
if (day>meets[x][meets[x].length-1] || meets[x][a]==0) {
if (month==11) {
december();
break;
}
month+=1;
day=0;
}
else if ((day==meets[x][a]) && (hrs <=14)) {
text="Our next meeting is today at 14:30 in the Club House";
break;
}

else if ((day==meets[x][a]) && (hrs >14)) {
addDay();
break;
}

else if (day<meets[x][a]) {
text="Our next meeting is at 14:30 on "+months[x]+ " "+meets[x][a]+ ", "+year + " in the Clubhouse."
break;
}
}
}
}

document.getElementById("date").innerHTML=text;
}

function addDay() {
d.setDate(d.getDate() + 1);
day = d.getDate();
getMeet();
}

function december () {
month=0;
day=0;
year+=1;
getMeet();
}

</script>
</body>
</html>

xelawho
01-30-2012, 04:12 PM
day+=1 works for me...



<html>
<head>
</head>
<body onload="getMeet()">
<div id="date"></div>
<script type="text/javascript">
var months=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
var meets = [[0],[0],[25],[0],[12,26,28],[14,28],[12,26],[25],[13,27],[11,25],[8,29],[13,27]];
var d=new Date("March 25, 2012 18:10")
var day=d.getDate()
var year=d.getFullYear()
var month=d.getMonth()

function getMeet(){
for ( x = 0; x < meets.length; x++ ){
for ( a = 0; a<meets[x].length; a++ ){
if(month==x){
if (day>meets[x][meets[x].length-1]||meets[x][a]==0){
if(month==11){
december();
break;
}
month+=1
day=0
}
else if(day==meets[x][a]){
if (d.getHours()>=18) {
day+=1
getMeet();
break;
}
text="Meeting is today"
break;
}
else if (day<meets[x][a]){
text="Next meeting is "+months[x]+ " "+meets[x][a]+ " "+year
break;
}
}
}
}
document.getElementById("date").innerHTML=text
}

function december () {
month=0
day=0
year+=1
getMeet()
}

</script>
</body>
</html>

Philip M
01-30-2012, 04:19 PM
day+=1 works for me...



var day = 31;
day += 1;
alert (day); //32:(

xelawho
01-30-2012, 04:27 PM
yes. slightly illogical, but it seems to me that


if (day>meets[x][meets[x].length-1]||meets[x][a]==0){
if(month==11){
december();
break;
}
month+=1
day=0
}



resets the "date" to day 0, month+=1 before 32 ever gets tested (because if day==32, it has to be greater than the date of the last meeting of the current month), or at least it seems that way, running the code...

Philip M
01-30-2012, 05:06 PM
Yes, that seems to be it. But I think I will stick with my method! Of course, as we often say, there is more than one way to skin a cat!

I think that really has made the script valuable to the user, and free of gremlins.

satcoucou
01-30-2012, 05:15 PM
<html>
<head>
</head>
<body>
<div id="date"></div>
<script type="text/javascript">

th problem that have in javascript

---------------
http://plombier-express.fr/

Philip M
01-30-2012, 05:38 PM
satcoucou - Please do not hijack someone else's thread. Prefer to start a new thread of your own. And say exactly what your problem is. You may well find that you can solve it if you take the trouble to Google for "Javascript Date Object".



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum