Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

Thread: Display Certain Messages depending on Date Range

1. Display Certain Messages depending on Date Range

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?

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

Thanks,

CJ

• are the meetings always on the same day of the month (ie, the 12th and the 26th) - if so it could be like this:

Code:
```<html>
<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>```

• Users who have thanked xelawho for this post:

CarabinerJ (01-26-2012)

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

Code:
```<html>

<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

• Users who have thanked Philip M for this post:

CarabinerJ (01-26-2012)

• 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

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

• ... or just put your meting dates into sub-arrays...

Code:
```<html>
<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>```

• A lot simpler than mine! Small correction:-

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

• 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!

Code:
```<html>
<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>```

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

Code:
```<html>
<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>```

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

• 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)

Code:
```<html>
<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>```

• Users who have thanked xelawho for this post:

Philip M (01-28-2012)

• 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?

• 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 course`var 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!

• Originally Posted by xelawho
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 course`var 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.

•
Page 1 of 2 12 Last