www.webdeveloper.com
Results 1 to 7 of 7

Thread: JavaScript to display next thursday

  1. #1
    Join Date
    Feb 2008
    Posts
    1

    JavaScript to display next thursday

    The company I work for does a weekly webinar every thursday. What I am trying to find is a script that will display the the following:

    "The next free online Webinar is:
    Thursday, XX, XXXX at 2 PM"

    If someone is logged onto my page on a Wednesday February 27, I need to display to him/her that the next demo will be on Thursday February 28. If the same person logs on on Friday February 29, I need to display to him the next demo date which will be on Thursday March 6. All this displaying the current date as well of course.

    Can anyone help? I know this is difficult but any help is appreciated.

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    It is not difficult. Here's an example:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    function whichNext(){
    var whichNext=4;//0=Sunday, 1=Monday ...
    var D=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
    var M=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
    var date=new Date();
    var dif=date.getDay()-whichNext;
    dif=dif>0?dif=7-dif:-dif;
    date.setDate(date.getDate()+dif);
    date.setHours(1);//DST pseudobug correction
    var dd=date.getDate();
    dd<0?dd='0'+dd:null;
    var yyyy=date.getFullYear();
    var DD=D[date.getDay()];
    var mm=M[date.getMonth()];
    document.getElementById('myspan').innerHTML=DD+', '+dd+', '+mm+', '+yyyy;
    }
    onload=whichNext;
    </script>
    </head>
    <body>
    <span id="myspan"></span>
    </body>
    </html>

  3. #3
    Join Date
    Feb 2006
    Posts
    2,927
    You cannot really rely on the user's clock, even for the day of the week,
    which is why most time calculations end up on the server.
    But usually the clock is ok, and a lot of webpages assume so.

    It is not finding next thursday- it is figuring out what 2 pm is to your visitor.
    You will need to use the GMT (Universal time) hours for your event's time;
    in this example I'm using EST (6 hours behind GMT)

    This function has default arguments based on your example,
    or you can pass it a new time (GMT hours and minutes) or day of the week.

    It returns a string suitable for the text content of an html element.


    Code:
    function getNextWeekday(UT,M,W){
    	UT=UT || 19;// get the GMT hours for this
    	M= M || 0; // minutes
    	W= W || 4; // week day index- thursday is 4 (Sunday is 0)
    
    	var daynames= ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
    	var monthnames= ['January','February','March','April','May','June','July',
    	'August','September','October','November','December'];
    
    	var str= 'The next meeting is scheduled for ';
    	var D= new Date(),h,m,p=' am';
    	var DD= D.getDay();
    
    	if(DD== W && D.getUTCHours()< UT) str+= ' Today '; // if it has not started yet
    	else{
    		while(D.getDay() < W) D.setDate(D.getDate()+1);
    		str+= daynames[W]+', '+monthnames[D.getMonth()] +' '+D.getDate();
    	}
    
    	// convert from UTC to local time
    	D.setUTCHours(UT,M);	
    	h= D.getHours();
    	m= D.getMinutes()+'';
    	if(m.length<2)m='0'+m;
    	if(h== 0) h= 12;
    	else if(h>12){
    		h-=12;
    		p=' pm'
    	}
    	return str+' at '+ h+':'+m+p ;
    }
    // test based on EST
    alert(getNextWeekday())
    Last edited by mrhoo; 02-28-2008 at 11:58 AM.

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    My code is easily adaptable for the server's clock, on inserting server-side variables into. But I don't think is a good idea. User needs to know when it is his next Thursday. If he has not settled the correct time on his computer, that will be his problem, and there is no way to check/correct that from outside.
    Last edited by Kor; 02-28-2008 at 01:51 PM.

  5. #5
    Join Date
    Feb 2006
    Posts
    2,927
    Oops.

    I blundered in my solution...
    The point is, if an event is going to be online at a specific time,
    that time has to be converted from GMT to local time.

    2 pm Thursday for me and my event may be 7pm in London, or 11am in California,
    or Wednesday in Australia.

    If you give the script a starting date that has been calculated as GMT,
    the local machine will immediately convert it to local time for all of its calculations.

    Otherwise, I'm afraid our mates in England will miss the show-
    not to mention Cleveland.

    Code:
    function getNextWeekday(S){
    // you could put S here instead of passing it from below
    	H= S.getHours();
    	M= S.getMinutes()+''; 
    	W= S.getDay(); 
    	// the day and time are localized by getting them as local from the GMT Date in S
    	str= 'The next event is scheduled for ';
    	var D= new Date(),h,m,p=' am';	
    	if(D.getDay== W && D.getHours()< H) str+= ' Today '; // if it has not started yet
    	else{
    		D.setDate(D.getDate()+1);
    		while(D.getDay() != W) D.setDate(D.getDate()+1);
    		str+= Date.daynames[W]+', '+Date.monthnames[D.getMonth()] +' '+D.getDate();
    	}
    	// get time string		
    	if(M.length<2)M='0'+M;
    	if(H== 0) H= 12;
    	else if(H>12){
    		H-=12;
    		p=' pm'
    	}
    	return str+' at '+ H+':'+M+p ;
    }
    // test based on EST
    var start =new Date(Date.UTC(2008,1,28,19,0,0,0))
    // This is the GMT for Thursday, Feb 28 2:00 pm-
    // you can set it at date, past or future
    // EST is 5 hours behind GMT on that date- it varies
    // You will need to set start from your own offset
    alert(getNextWeekday(start))


    Oh, almost forgot-
    Code:
    Date.daynames= ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
    Date.monthnames= ['January','February','March','April','May','June','July',
    'August','September','October','November','December'];
    Last edited by mrhoo; 02-28-2008 at 04:04 PM.

  6. #6
    Join Date
    Jan 2014
    Posts
    1
    The above code works great, but how can you pick every other week. I have tried, but no luck. Thx ahead for any help.

  7. #7
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,498
    Use PHP for this and the web serve clock, this assumes that the web provider has set the proper time, you find as I have allot of american budget hosts don't bother setting the server time or putting in services to syncronise their server clocks. If this is paid for rackspace and your companies server being hosted in a farm, then your server time will be whatever your company IT department set before shipping or if they bought a rack space, whatever the IT department of the farm set if at all...

    Server clocks are just as prone to be wrong, however ... established web time services that give GMT times are accurate and set, so referencing them once a week for example is a good idea and knowing how far ahead / behind GMT you are would allow you to set your server to your local time.

    PHP does offer time zone based settings, this again takes its timings from the server...

    As for JavaScript, it takes its time from the users operating system which generally for the individuals needs will have a clock set to their local time, this however does not help you in offering a service based on your Thursday Webinar
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles