PDA

View Full Version : Text changes based on date


Gordo
08-17-2002, 08:34 AM
I've searched, I'm tired, and now I'm asking for help.

PLEASE provide complete code in any examples you provide -- thanks.

Using JavaScript, I need something that would accomplish the following.

I have text on a page that says "Our next meeting is Tuesday, August 20th". Now, after the 20th, I need the text to say (for example only) "Our next meeting is Tuesday, August 27th". The day and date would not be each and every Tuesday. I'd need to be able to put this designation in the JS.

So, what I need is for the page to change the text for the next meeting date based upon the current date. If today is the 21st, then the 20th should not appear. What should appear is whatever is the "next" date that I've coded into the JS. Understand?

I hope I made sense. I just don't want to have to go back and update this web page each and every week. I want the date for the forthcoming meeting to be displayed immediately after the previous meeting date has passed. This just needs to be based on the day (not the hour/minute).

scroots
08-17-2002, 01:46 PM
this is something easy to acheive, i`ll find the code and post it.

scroots

thickandthin
08-18-2002, 12:59 AM
i can help you i saw it on a web site the other day ill look for it

thickandthin
08-18-2002, 02:09 AM
i hope it works sorry if it doesent, just tell me if it doesent and ill try again


<SCRIPT LANGUAGE="JavaScript">



today = new Date();
day = today.getDay();
arday = new Array("Sunday stuff", "moday stuff", "tuesday stuff",
"wednesday stuff", "thursday stuff", "friday stuff", "saturday stuff");

document.write( arday[day] );

</script>

justame
08-18-2002, 12:42 PM
gor...
theres just a lotsŪ of 'em linked from here...

http://www.google.ca/search?q=different+messages+on+dates+javascript&ie=ISO-8859-1&hl=en&meta=

thickandthin
08-18-2002, 03:46 PM
i looked there a dozen times!

Gordo
08-19-2002, 04:24 AM
I, too, searched Google several times with minimal relevant results.

BTW - Thanks for the replies and code. But, I don't need a different message for each day. I need to give the same message each day...up until a certain date has passed (several dates per month...basically weekly).

Let me see if I can be a little more detailed in my request.

For the simple version of this, I would need the date (like "Tuesday, August 20th") to be determined by the PC/page.

If the actual date were before the 20th, then the text message should read "Our next meeting is Tuesday, August 20th".

If the actual date were after the 20th, then the text message should read ""Our next meeting is Tuesday, August 27th".

This version would basically just write the next Tuesday's date.

A complex version would allow me to use days other than Tuesdays, etc...and would allow me to write more info for a particular upcoming date. But, I don't need this complex version just yet (if ever).

Does that help?

Gordo
08-20-2002, 03:57 AM
<bump>
Didn't think this would be too hard for the JSxperts.
:confused:
</bump>

whammy
08-20-2002, 05:22 AM
Geez, where is Dave Clark? I can do this, but unfortunately I don't have time right now darn it. I have to go to bed because I have to go to work in 4.5 hours :-/

If I were you I'd just look up some time scripts and think about what you're trying to accomplish...

I really do love to tackle this kind of thing but I just don't have time at the moment :(

joh6nn
08-20-2002, 06:34 AM
this won't wrap around in december, but otherwise, it should be pretty good.

<script>
var Schedule = new Array(12);
for ( var i in Schedule ) {
Schedule[i] = new Array();
}
var weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];


Schedule[7][20] = ["12:30 PM" , "Some info for that day"]; // Set the days you want

function dateWriter() {
var today, then, start;
today = new Date();
if ( Schedule[today.getMonth()][today.getDate()] ) {// only true if you explicitly set it
return ("Meeting today at" + Schedule[today.getMonth()][today.getDate()][0] + ", the topic is " + Schedule[today.getMonth()][today.getDate()][1] + ".");
}
else {
for (var m = today.getMonth(); m < 12; m++) {
start = ( m == today.getMonth() ) ? today.getDate() : 1;
for (var d = start; d < 31; d++) {
if (Schedule[today.getMonth()][today.getDate()]) {
then = new Date(today.getYear(), m, d);
return ("Next Meeting on" + weekdays[then.getDay()] + ", " + months[m] + " " + d + ", at " + Schedule[m][d][0] + ". The topic will be " + Schedule[m][d][1] + ".");
}
}
}
}

document.write(dateWriter());
</script>

Gordo
08-20-2002, 06:56 AM
I'm getting the following error:

Expected '}'

whammy
08-20-2002, 10:16 PM
Hmm, I fixed that by adding a } before the closing script tag... but then I get a lot more errors! :D

It's a shame you don't use ASP, something like this would be really simple...

Gordo
08-20-2002, 11:48 PM
No, I've never used ASP, but would be willing to cut-n-paste anything that's offered. I'll try just about anything once!

[misses Dave Clark]

joh6nn
08-21-2002, 03:52 AM
sorry, denkfehler. try it now:

<script>
var Schedule = new Array(12);
for ( var i = 0; i < 12; i++ ) {
Schedule[i] = new Array();
}
var weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];


Schedule[7][20] = ["12:30 PM" , "Some info for that day"]; // Set the days you want

function dateWriter() {
var today, then, start;
today = new Date();
if ( Schedule[today.getMonth()][today.getDate()] ) {// only true if you explicitly set it
return ("Meeting today at" + Schedule[today.getMonth()][today.getDate()][0] + ", the topic is " + Schedule[today.getMonth()][today.getDate()][1] + ".");
}
else {
for (var m = today.getMonth(); m < 12; m++) {
start = ( m == today.getMonth() ) ? today.getDate() : 1;
for (var d = start; d < 31; d++) {
if (Schedule[today.getMonth()][today.getDate()]) {
then = new Date(today.getYear(), m, d);
return ("Next Meeting on" + weekdays[then.getDay()] + ", " + months[m] + " " + d + ", at " + Schedule[m][d][0] + ". The topic will be " + Schedule[m][d][1] + ".");
}
}
}
}
}

document.write(dateWriter());
</script>

Gordo
08-21-2002, 05:12 AM
Dave Clark's not here...but joh6nn sure is!

denkfehler --- I had to look that one up.

Okay, this is confusing me a little, and may not get the job done as is (yet). Let me see if I can explain further.

With Schedule[7][20] = ["12:30 PM" , "Some info for that day"]; // Set the days you want, is that supposed to be "7" for "August"? If so, isn't August the 8th month (not 7th)? The 20th I get...as today is (as I'm typing this) the 20th.

I tested it a bit prior to cutting some of the extra text out. Anyway, this is my current code:

<script>
var Schedule = new Array(12);
for ( var i = 0; i < 12; i++ ) {
Schedule[i ] = new Array();
}
var weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

// Set the days you want
Schedule[7][20] = ["Tuesday, August 20th." , "Some info for that day"];
Schedule[8][27] = ["Tuesday, August 27th." , "More info can be put here -- THIS IS NOT CURRENTLY BEING USED"];
Schedule[8][3] = ["Tuesday, September 3rd." , "More info can be put here"];
Schedule[8][10] = ["Tuesday, September 10th." , "More info can be put here"];
Schedule[8][17] = ["Tuesday, September 17th." , "More info can be put here"];
Schedule[8][24] = ["Tuesday, September 24th." , "More info can be put here"];

function dateWriter() {
var today, then, start;
today = new Date();
if ( Schedule[today.getMonth()][today.getDate()] ) {// only true if you explicitly set it
return (Schedule[today.getMonth()][today.getDate()][0]);
}
else {
for (var m = today.getMonth(); m < 12; m++) {
start = ( m == today.getMonth() ) ? today.getDate() : 1;
for (var d = start; d < 31; d++) {
if (Schedule[today.getMonth()][today.getDate()]) {
then = new Date(today.getYear(), m, d);
return (weekdays[then.getDay()] + ", " + months[m] + " " + d + ", at " + Schedule[m][d][0] + ". The topic will be " + Schedule[m][d][1] + ".");
}
}
}
}
}

document.write(dateWriter());
</script>

What I'm trying to do is so that the message will tell the NEXT meeting date. So, if today is Wednesday the 21st, then the message should say that the next meeting is "Tuesday, August 27th." But as it is right now, all I get is "undefined". Basically, the message needs to tell the next meeting date no matter what day the visitor comes to the page. If the last meeting (date) has passed, then the code should write the next (forthcoming) date. Currently, all future meetings are on every Tuesday, but that could change. Therefore, I like the way I can (possibly) change this Schedule to fit. I would NOT want it hard-coded for Tuesdays only.

AGAIN -- I really appreciate your persistence in this matter!

NOTE: Had to put a space in the [i ] about the third line of code...kept making everything italic. Don't know the posting work-around.

joh6nn
08-21-2002, 06:48 PM
when you make a date object, and do theDate.getMonth();, it returns a number between 0 and 11, which makes August the 7th month. confusing, i know.

i sorted through the code again, and noticed another denkfehler on my part. this however, works:

<script>
var Schedule = new Array(12);
for ( var i = 0; i < 12; i++ ) {
Schedule[i ] = new Array();
}
var weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

// Set the days you want
Schedule[7][20] = ["Tuesday, August 20th." , "Some info for that day"];
Schedule[7][27] = ["Tuesday, August 27th." , "More info can be put here"];
Schedule[8][3] = ["Tuesday, September 3rd." , "More info can be put here"];
Schedule[8][10] = ["Tuesday, September 10th." , "More info can be put here"];
Schedule[8][17] = ["Tuesday, September 17th." , "More info can be put here"];
Schedule[8][24] = ["Tuesday, September 24th." , "More info can be put here"];

function dateWriter() {
var today, then, start;
today = new Date();
if ( Schedule[today.getMonth()][today.getDate()] ) {// only true if you explicitly set it
return (Schedule[today.getMonth()][today.getDate()][0]);
}
else {
for (var m = today.getMonth(); m < 12; m++) {
start = ( m == today.getMonth() ) ? today.getDate() : 1;
for (var d = start; d < 31; d++) {
if (Schedule[m][d]) {
then = new Date(today.getFullYear(), m, d);
return (weekdays[then.getDay()] + ", " + months[m] + " " + d + ", at " + Schedule[m][d][0] + ". The topic will be " + Schedule[m][d][1] + ".");
}
}
}
}
}

document.write(dateWriter());
</script>


the way i wrote the code, when you set up the dates, you don't need to put it in like this:
Schedule[7][27] = ["Tuesday, August 27th." , "More info can be put here"];
it does that by itself. i was trying to keep it as flexible as possible, so that as little information as possible would need to be hard coded.

hope that this time it does what you're after.

Gordo
08-22-2002, 03:00 AM
<EDIT>
READ THIS FIRST...left my original reply so you and others could see my last query.

I think I fixed it myself. This last request for assistance was probably a little premature.

I changed the "if" statement to the following:
if ( Schedule[today.getMonth()][today.getDate()] ) {// only true if you explicitly set it
return ("today at " + Schedule[today.getMonth()][today.getDate()][0]);
}

Tell me if that's wrong. Otherwise, I think we've gotten this one licked. I'm thrilled!
</EDIT>


I'm starting to feel it! We're almost there.

Okay, I have the following line (for example):
Schedule[7][27] = ["7:00pm" , "blah blah blah"];
This is for August 27th.

Well, when I change my PC to the 27th, the script spits out 7:00pm

So, the "if" statement is causing it to just spit out the first part, and leaving off the whole actual date. I need the date of an event to read just like the day prior to the event. It should still say Tuesday, August 27th, at 7:00pm. The topic will be blah blah blah.

So, can you adjust this for me!?

THANKS SO MUCH!!!

joh6nn
08-22-2002, 03:26 AM
yeah, that should be fine. doesn't really matter, as long as the thing works, right? ::grin::

you might want to change it to this, though:

if ( Schedule[today.getMonth()][today.getDate()] ) {// only true if you explicitly set it
return ("today at " + Schedule[today.getMonth()][today.getDate()][0] + '. The topic is ' + Schedule[today.getMonth()][today.getDate()][1]+'.');
}

RadarBob
08-22-2002, 04:20 AM
OK. Now that y'all have been at this for a few days, I'm gonna give my plug nickel's worth.

Flexible code as has been stated is definitely a big consideration. So I'm beginning to think "objects."

I think y'all are on the right track with the arrays that hold the names of the months and days. However my spidey-sense was tingling when I saw the Schedule array that tied the date to the array index.

Since it's my beddy-bye time I'm not going to cut code till tomorrow but I will say what I see is your requirements. Please correct any errors:
* From today's date (local time), you want to print out the date of the very next meeting
* you want to print that date "spelled out", and in local time.
* Meetings can be at any time in the future. Y'all just happen to schedule them on Tuesday. I.E. meetings are not *guaranteed* to come every Tuesday, regardless of holidays, etc.
* The design must accomodate meeting changes.

OK. I'll sleep on it. I have a couple of ideas.

Gordo
08-22-2002, 04:32 AM
Thanks joh6nn!

RadarBob, I'm always open to new solutions, so give it your best shot. As for the requirements you listed, you've got a good handle on it. The only adjustment I'd probably make is spelling it out in "local time". I don't know if you're getting too deep or if that's just the term you decided to use. To me, that says it'll try to differentiate between Eastern, Central, Mountain, Pacific time, etc. Anyway, I just need it to tell the next meeting day (which I would set as in the example above). joh6nn's example even added a "topic" info to be added each time. I don't need that at the present, but may very well in the future. So, I can always leave it blank for now. Now I'm starting to ramble -- just wish I could go ahead and get some sleep. Unfortunately, I've been staying up until 4-5am each morning working on the PC.

RadarBob
08-22-2002, 09:03 PM
OK, folks here's what I came up with.

I wanted to play around with objects. Results are fair, I think.

About the design
I decided to work with the "fact" that meetings happen on a regular schedule. For example every Friday.

I thought about allowing for meetings at any time but I didn't see how to do that without manually maintaining a two-dimentional array as seen previously in this thread. The more I considered it the more of a pain in the neck that was!

So anyway, I designed an object - "meeting" which needs 3 things when created: (1) how often are meetings? (2) what units are we talking about (days, weeks, months) and (3) what start date can I use to base my calculations on?

so If my first meeting was on Aug 2, 2002 and I hold meetings every two weeks I create an object like this:
var myMeeting = new meeting(2, "week", "Aug 2, 2002");


The beauty of this is the line above is the only line you ever have to change.

bottom line
The code you need to write to use the meeting object borders on the trivial. If your meetings schedule changes there's only one line of code to modify.

There seems to be a lot of code for the object. But it's not complex. Generally compare it to the 2-dim array code earlier.

Stuff I learned doing this
Dates is a complex issue! Take a look at the date & time stuff on this site:
http://www.merlyn.demon.co.uk/js-index.htm

Making objects with JS objects as properties can be confusting. For example, if "meetingDate" was a date, I could not reference JS date functions like this:
this.meetingDate.getDate()
I had to do this first:
var tempDate = this.meetingDate;
tempDate.getDate();

I really don't have to call "calcNextMeeting()" once for each piece of the date. I could do it all in one function.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Next Meeting Date Calculation</title>

<script language="JavaScript1.3" type="text/javascript">
<!--
// the "meeting" object
function meeting(frequency, units, baseDate) {
this.howOften = frequency;
this.whatUnit = units;
this.startDate = new Date (baseDate);
this.today = new Date();

// object methods
this.nextMeeting = calcNextMeeting;
this.meeting_day = getCalendarDay;
this.meeting_date = getCalendarDate;
this.meeting_month = getCalendarMonth;
this.meeting_year = getCalendarYear;
} //object meeting()


function calcNextMeeting () {
meetWhen = new Date (this.startDate);

while (meetWhen <= this.today) {
switch (this.whatUnit) {
case 'day':
meetWhen.setDate(meetWhen.getDate() + this.howOften);
break;

case 'week':
meetWhen.setDate(meetWhen.getDate() + (this.howOften * 7));
break;

case 'month':
meetWhen.setMonth(meetWhen.getMonth() + (this.howOften));
break;

default:
meetWhen = null;
alert (whatUnit + "is invalid\n" +
"Meetings are scheduled by \'day\', \'week\' or \'month\'");
break;
}
} // while()
return meetWhen
} // function calcnextMeeting()

function getCalendarYear () {
var tempDate = this.nextMeeting();
var thisYear = tempDate.getFullYear();
return thisYear;
}

function getCalendarMonth () {
var tempDate = this.nextMeeting();
var thisMonth = tempDate.getMonth()
var months = new Array ("January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December");
return months[thisMonth];
} //function getCalendarMonth()


function getCalendarDay () {
var tempDate = this.nextMeeting();
var thisDay = tempDate.getDay();
var days = new Array ("Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday");
return days[thisDay];
} //function getCalendarDay()

function getCalendarDate () {
var tempDate = this.nextMeeting();
var thisDate = tempDate.getDate();
return thisDate;
}// function getCalendarDate()

//-->
</script>

</head>
<body>

<script language="JavaScript1.3" type="text/javascript">
<!--
/*
The parameters creating the meeting object mean:
meet once per week starting on Aug 2, 2002.
*/
var ourMeeting = new meeting(1, "week", "Aug 2, 2002");

document.write("<BR> Today is ");
document.write (ourMeeting.today);
document.write ("<BR>Our next meeting is on <b>" +
ourMeeting.meeting_day() + " " +
ourMeeting.meeting_month() + " " +
ourMeeting.meeting_date() + ", " +
ourMeeting.meeting_year() + "</b>"
);
//-->
</script>

</body>
</html>

Gordo
08-23-2002, 03:34 AM
Wow, RadarBob, that is MOST interesting. I'll have to play around with it (just the dates, etc.) and see which code (yours or joh6nn's) to use.

Man, I love this forum!

Hopefully I'm not speaking too soon, but consider this problem/thread solved!