Connecting Tech Pros Worldwide Help | Site Map

make a time script shorter

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 20th, 2005, 10:56 AM
Treetop
Guest
 
Posts: n/a
Default make a time script shorter

I used the following script for a site to stop displaying after a date /
time, but it seems huge. Any ideas on how to make it shorter? There are
many events in this list.


function events() {

var today = new Date();
var dayarray=new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
var montharray=new
Array("Jan","Feb","Mar","Apr","May","Jun","Jul","A ug","Sep","Oct","Nov","Dec
")


document.write('<table>');
var event = "Spirit of the Dance - 8:00 pm (<a
href=ticketorderform_spiritofthedance.html>Order Tickets</a>)";
var date = new Date("October 30, 2003");
date.setHours (23);
date.setMinutes (21);
date.setSeconds (59);
var year=date.getYear()
if (year < 1000)
year+=1900
var day=date.getDay()
var month=date.getMonth()
var daym=date.getDate()
var cdate=dayarray[day]+", "+montharray[month]+" "+daym+" "+year+" "
if (today.getTime() <= date.getTime()) {
document.write('<tr><td valign=top>');
document.write(cdate);
document.write('</td><td>- ');
document.write(event);
document.write('</td></tr>');
}


document.write('</table>');

}



  #2  
Old July 20th, 2005, 10:56 AM
Dr John Stockton
Guest
 
Posts: n/a
Default Re: make a time script shorter

JRS: In article <ac322d9e5e3a1cf579182abefa5b0e62@news.teranews.co m>,
seen in news:comp.lang.javascript, Treetop <treetop@netfront.net> posted
at Fri, 3 Oct 2003 16:42:30 :-[color=blue]
>I used the following script for a site to stop displaying after a date /
>time, but it seems huge. Any ideas on how to make it shorter? There are
>many events in this list.
>
>
>function events() {
>
>var today = new Date();
>var dayarray=new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat")[/color]
[color=blue]
>var dayarray=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"][/color]
[color=blue]
>var montharray=new
>Array("Jan","Feb","Mar","Apr","May","Jun","Jul"," Aug","Sep","Oct","Nov","Dec
>")
>
>
>document.write('<table>');
>var event = "Spirit of the Dance - 8:00 pm (<a
>href=ticketorderform_spiritofthedance.html>Orde r Tickets</a>)";
>var date = new Date("October 30, 2003");
> date.setHours (23);
> date.setMinutes (21);
> date.setSeconds (59);[/color]

var date = new Date(2003,9,30,23,21,59)
var date = new Date("2003/10/31") // near enough ?
var date = new Date("2003/10/30 23:21:59")
[color=blue]
>var year=date.getYear()
>if (year < 1000)
>year+=1900[/color]

var year = 1900 + date.getYear()%1900 // < AD 3800
[color=blue]
>var day=date.getDay()
>var month=date.getMonth()
>var daym=date.getDate()
>var cdate=dayarray[day]+", "+montharray[month]+" "+daym+" "+year+" "
>if (today.getTime() <= date.getTime()) {
>document.write('<tr><td valign=top>');
>document.write(cdate);
>document.write('</td><td>- ');
>document.write(event);
>document.write('</td></tr>');
> }[/color]

if (today <= date) with (date)
document.write('<tr><tc valign=top>, dayarray[getDay()], ', ',
montharray[getMonth()], ' ', getDate(), ' ',
1900 + date.getYear()%1900, '<\/td><td>- ', event, '<\/td><\/tr>')[color=blue]
>
>
>document.write('</table>');
>
>}[/color]

After testing, then shorten variable names.

For World-Wide Web use, M D Y is silly; use Y M D. Consider LZ, or
equivalent, to add leading zero to getDate().

There is no point in calculating anything that will not get used; test
the date earlier.



To shorten repetitive work, use a function with parameters. For
example, function WriteTable(Evnt, Till) ; today, dayarray,
montharray can be global. If the Evnt always has a similar URL,
parameterise the variable part of that, too :-

WT("Spirit of the Dance - 8:00 pm",
"spiritofthedance", "2003/10/30 23:21:59")

Then you can, for a slight gain on a long schedule, supply those as
elements of an array :

var Data = [
["Spirit of the Dance - 8:00 pm",
"spiritofthedance", "2003/10/30 23:21:59"],
["Mozart - 15:30", "wam", "2003/12/31 11:45"], ... ... ]



Read the FAQ, thoughtfully.



Note that the event occurs at a specific time and location; you want to
prevent ticket ordering at that absolute time or a fixed offset from
that, independently of the orderer's location. The chronologically
retarded, such as Alaskans, should not be able to take undue advantage;
the advanced Maoris should not be handicapped.

It is still possible, I think, to be using a computer
in the VIP lounge at LHR, and to arrive in or near NYC
at an earlier local time.



NOTE to all, prompted by above :

<URL:http://www.merlyn.demon.co.uk/js-date5.htm#DDTA> is much changed;
it could do with a functionality test on later browsers, a correctness
check, and any suggestions for reasonable code reduction. I think the
page displays all relevant code, except imports.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.