From: David J Trower on 21 Jul 2010 21:31 I am not that familiar with JavaScript, just enough that I can piece together a script when looking at some examples that are similar to what I want. However, I have reached a snag. I am working on a script that I can have a list of multiple upcoming events, and the JavaScript go through them and when it finds the one that should currently be shown, displays it. I have the script working if I have only one event in the code for it to parse, but I am wanting to avoid having to edit the code on a weekly basis. Below is my code as it currently stands. Any help would be greatly appreciated. // var expireEvents = "06-24-2010-13-00-07-01-2010-12-59-Upcoming Program-Greg Evans, 2010-2011 Northwest Waco Rotary President-July 1, 2010"; // var expireEvents = "07-01-2010-13-00-07-08-2010-12-59-Upcoming Program-Kurt Slaughter, Texas Parks and Wildlife Game Warden-July 8, 2010"; // var expireEvents = "07-08-2010-13-00-07-15-2010-12-59-Upcoming Program-Mike Cain, Oncor Area Manager-July 15, 2010"; var expireEvents = "07-15-2010-13-00-07-22-2010-12-59-Upcoming Program- TBA-July 22, 2010"; // var expireEvents = "07-22-2010-13-00-07-29-2010-12-59-Upcoming Program-Jason Jennings, Hillcrest COO-July 29, 2010"; // var expireEvents = "07-29-2010-13-00-08-05-2010-12-59-Upcoming Program-Wes Allison, HOT Fair-August 5, 2010"; // var expireEvents = "08-05-2010-13-00-08-12-2010-12-59-Upcoming Program-John Morris, 2010 Baylor Football Preview-August 12, 2010"; // var expireEvents = "08-12-2010-13-00-08-15-2010-00-00-Upcoming Event-Beach Party with <em>The Morticians</em>-August 14, 2010"; // var expireEvents = "07-20-2010-15-30-07-25-2010-22-29-Upcoming Program-Guest Speaker, XYZ Company-July 20, 2010"; var expireEvents = expireEvents.split('-'); var goLiveMonth; var goLiveDay; var goLiveYear; var goLiveHour; var goLiveMinute; var expireMonth; var expireDay; var expireYear; var expireHour; var expireMinute; var expireDST; var exeventHeader; var exeventSpeaker; var exeventDate; var goLiveDate; var expireDate; var mycontent; goLiveMonth = expireEvents[0]; goLiveDay = expireEvents[1]; goLiveYear = expireEvents[2]; goLiveHour = expireEvents[3]; goLiveMinute = expireEvents[4]; expireMonth = expireEvents[5]; expireDay = expireEvents[6]; expireYear = expireEvents[7]; expireHour = expireEvents[8]; expireMinute = expireEvents[9]; exeventHeader = expireEvents[10]; exeventSpeaker = expireEvents[11]; exeventDate = expireEvents[12]; var goLiveDate = goLiveYear + goLiveMonth + goLiveDay + ' ' + goLiveHour + ':' + goLiveMinute; var expireDate = expireYear + expireMonth + expireDay + ' ' + expireHour + ':' + expireMinute; var mycontent = '<div id="labelzone"><br /><h3>' + exeventHeader + '</ h3><p>' + exeventSpeaker + '<br />' + exeventDate + '</p></div><!-- #labelzone-->'; var nowDate = new Date(); var day = nowDate.getDate(); var month = nowDate.getMonth(); var correctedMonth = month + 1; //month - JavaScript starts at "0" for January, so we add "1" if (correctedMonth < 10) { /* if less than "10", put a "0" in front of the number. */ correctedMonth = "0" + correctedMonth; } if (day < 10) { /* if less than "10", put a "0" in front of the number. */ day = "0" + day; } var year = nowDate.getYear(); /* Get the year. Firefox and Netscape might use century bit, and two-digit year. */ if (year < 1900) { year = year + 1900; /*This is to make sure Netscape AND FireFox doesn't show the year as "107" for "2007." */ } var hour = nowDate.getHours(); /* Get the hour. */ if (hour < 10) { hour = "0" + hour; } var minute = nowDate.getMinutes(); /* Get the minute. */ if (minute < 10) { minute = "0" + minute; } var GMTdate = year + "" + correctedMonth + "" + day + " " + hour + ":" + minute; //corrected month GMT date. if ((GMTdate <= expireDate) && (GMTdate >= goLiveDate)) { document.write(mycontent) } -- David Trower Web Designer e-mail: jdavidtrower(a)gmail.com
From: Mike Duffy on 21 Jul 2010 23:46 David J Trower <christluvsu2(a)gmail.com> wrote in news:fa1aba3f-2973-4682-b063-35a37092db7c(a)s9g2000yqd.googlegroups.com : > .... Below is my code as it currently stands. > Any help would be greatly appreciated. Why not use the standard Javascript date format for your date strings? Then you could just use standard date parsing. The first event would look something like: expireEvents = { "Jun 24 2010 13:00", "Ju1 01 2010 12:59", "Greg Evans", "Northwest Waco Rotary" }; If you use this format for the dates, then you can get rid of most of the other code and replace it with GMTdate = new Date(); expireDate = new Date (Date.parse(expireEvents[1])); goliveDate = new Date (Date.parse(expireEvents[0])); if ((GMTdate >= goLiveDate) && (GMTdate < expireDate)) { document.write(expireEvents[2] + expireEvents[3] + "htm fmts etc." ) }; -- http://pages.videotron.com/duffym/index.htm
From: Thomas 'PointedEars' Lahn on 22 Jul 2010 04:54 David J Trower wrote: > [...] I am working on a script that I can have a list of multiple > upcoming events, and the JavaScript go through them and when it finds the > one that should currently be shown, displays it. I have the script > working if I have only one event in the code for it to parse, but I am > wanting to avoid having to edit the code on a weekly basis. [...] > > // var expireEvents = "06-24-2010-13-00-07-01-2010-12-59-Upcoming > Program-Greg Evans, 2010-2011 Northwest Waco Rotary President-July > 1, 2010"; > [...] > // var expireEvents = "07-20-2010-15-30-07-25-2010-22-29-Upcoming > Program-Guest Speaker, XYZ Company-July 20, 2010"; > > var expireEvents = expireEvents.split('-'); > var goLiveMonth; > […] > var exeventDate; > var goLiveDate; > var expireDate; > var mycontent; > > goLiveMonth = expireEvents[0]; > […] > exeventDate = expireEvents[12]; That is a terrible waste, and easily leads to inconsistent code. Consider instead var goLiveMonth = expireEvents[0]; … var exeventDate = expireEvents[12]; or to save even more typing and storage: var goLiveMonth = expireEvents[0], … exeventDate = expireEvents[12]; Even better, consider using an object being initialized in a loop: var aParts = ["goLiveMonth", "goLiveDay", …], oEvent = {}; for (var i = aParts.length; i--;) { oEvent[aParts[i]] = expireEvents[i]; } or, if you find that more intuitive (although due to the function calls it is probably less efficient), aParts.forEach(function (e, i) { oEvent[e] = expireEvents[i]; }); (the latter being natively available only in newer implementations.) If you are talking JavaScript 1.7 and later, you can even do var [goLiveMonth, goLiveDay, …] = expireEvents.split('-'); but, as I indicated before, I recommend to avoid declaring too many variables. > var year = nowDate.getYear(); /* Get the year. Firefox and Netscape > might use century bit, and two-digit year. */ > if (year < 1900) { > year = year + 1900; /*This is to make sure Netscape AND FireFox > doesn't show the year as "107" for "2007." */ > } Use nowDate.getFullYear() instead of this crude, error-prone workaround. BTW, Netscape development and support has been ended by its vendor AOL two years ago: <http://browser.netscape.com/> As for your question, though, simply do not use only string values: var expireEvents = [ { goLive: new Date(2010, 5, 24, 13), expire: new Date(2010, 6, 1, 12, 59), header: "Upcoming Program", speaker: "Greg Evans, 2010-2011 Northwest Waco Rotary President", date: new Date(2010, 6, 1) }, … ]; (If you find the `Date' constructor confusing, write a wrapper function that you call instead. Probably the `goLive' and `expire' values do not need to be hard-coded in the first place. You could have a constructor that computes those values depending on the date passed to it.) `expireEvents' then stores a reference to an Array instance that has references to Object instances as its elements that you can iterate over. You really want to do this server-side (at least partially), though, regardless of the programming language you would use then. Please read the FAQ of this newsgroup before asking further questions: <http://jibbering.com/faq/> HTH PointedEars -- var bugRiddenCrashPronePieceOfJunk = ( navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1 ) // Plone, register_function.js:16
From: Thomas 'PointedEars' Lahn on 22 Jul 2010 05:10 Mike Duffy wrote: > David J Trower wrote: >> .... Below is my code as it currently stands. >> Any help would be greatly appreciated. > > Why not use the standard Javascript date format for your date strings? > The first event would look something like: > > expireEvents = { "Jun 24 2010 13:00", "Ju1 01 2010 12:59", "Greg > Evans", "Northwest Waco Rotary" }; You don't know what you are talking about (which is unsurprising for a person who anti-socially blocks e-mail communication on Usenet, though). There is no "Javascript" to begin with, much less a "standard Javascript". As for standards, ECMAScript Edition 5: | 15.9.1.15 Date Time String Format | | ECMAScript defines a string interchange format for date-times based upon a | simplification of the ISO 8601 Extended Format. The format is as follows: | YYYY-MM-DDTHH:mm:ss.sssZ | [...] | | 15.9.4.2 Date.parse (string) | | The parse function applies the ToString operator to its argument and | interprets the resulting String as a date and time; it returns a Number, | the UTC time value corresponding to the date and time. The String may be | interpreted as a local time, a UTC time, or a time in some other time | zone, depending on the contents of the String. The function first attempts | to parse the format of the String according to the rules called out in | Date Time String Format (15.9.1.15). If the String does not conform to | that format the function may fall back to any implementation-specific ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | heuristics or implementation-specific date formats. Unrecognizable Strings ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | or dates containing illegal element values in the format String shall ^^^^^ | cause Date.parse to return NaN. [...] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ And ECMAScript Edition 3 Final: | 15.9.4.2 Date.parse (string) | | The parse function applies the ToString operator to its argument and | interprets the resulting string as a date; it returns a number, the UTC | time value corresponding to the date. The string may be interpreted as a ^^^ | local time, a UTC time, or a time in some other time zone, depending on | the contents of the string. | | If x is any Date object whose milliseconds amount is zero within a | particular implementation of ECMAScript, then all of the following | expressions should produce the same numeric value in that implementation, | if all the properties referenced have their initial values: | | x.valueOf() | Date.parse(x.toString()) | Date.parse(x.toUTCString()) | | However, the expression Date.parse(x.toLocaleString()) is not required to | produce the same number value as the preceding three expressions and, in | general, the value produced by Date.parse is implementation-dependent when | given any string value that could not be produced in that implementation | by the toString or toUTCString method. Guess how many conforming implementations of ES 3 (2000 CE) and how many of ES 5 (2010 CE) are out there. Guess again. PointedEars -- Anyone who slaps a 'this page is best viewed with Browser X' label on a Web page appears to be yearning for the bad old days, before the Web, when you had very little chance of reading a document written on another computer, another word processor, or another network. -- Tim Berners-Lee
From: David Mark on 22 Jul 2010 06:25
On Jul 22, 4:54 am, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> wrote: [...] > date: new Date(2010, 6, 1) Careful on this one. In rare cases, in some time zones, this can underflow to the previous day (and month in this example). Has to do with DST, IIRC. Of course, that won't matter unless you are going to display it, convert it to a string, retrieve the day, etc. In other words, if it will only be used to calculate time spans, it's fine. I have code that fixes this and will post if there is any interest. I once attempted to use it to fix a bug in the Dojo calendar; but, as usual they went into "show me where it fails" mode. They understood the underlying problem, but could not grasp why they should not change the Date object that is passed to the widget's constructor and exposed as a property (or retrieved by a method). It seemed like a simple enough concept to me: GIGO. Don't screw with it until you have to display it (and then screw with a copy, not the original). IIRC, their meddlesome solution was a line or two shorter and "worked" as they saw it. :( |