Wrox Programmer Forums
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 4th, 2006, 03:29 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default clock script question

Hi,

I am currently writing a page that handles food orders and sends the requests of customers to our restaurant (its intranet based). What I am trying to write at the moment is a script that gives all possible food collection times up to closing time, depending on the current time. Here's the script:

<script language = "javascript">


var tNow = new Date()
var minutes = tNow.getMinutes() + 15
var hours = tNow.getHours()




            for(i = 1; i <=?; i++) //What goes in place of the question mark???
            {



                        if(minutes > 60)
                        {
                        hours+=1
                        minutes-=60
                            if(minutes <=9)
                            {
                            minutes = "0" + minutes
                            }
                            if(minutes ==10)
                            {
                            minutes = minutes + "0"
                            }
                        }
                        document.getElementById("tSelect").options[i] = new Option(hours + ":" + minutes)




</script>

So as you can see Im trying to populate a select box with 15 minute intervals. What I'm trying to work out is the maths. If its 8am and the latest collection time is 12pm then there would be 4 15 minute slots for each of the 4 hours between 8 and twelve giving you 16 15 minute slots. Problem is I'm having real trouble converting the maths to javascript. Getting myself confused! Any help always appreciated.

Joe
__________________
\'sync\' &lt;cr&gt;
The name specified is not recognized as an internal or external command, operable program or batch file.
 
Old July 12th, 2006, 05:51 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Joe,

If you're doing a lot of maths with dates, I suggest you use a library for date computation rather than writing one yourself. For adding to and subtracting from dates, download Yahoo's Calendar widget (http://developer.yahoo.com/yui/calendar/) and use the YAHOO.widget.DateMath class. It provides an API to add and subtract hours, minutes, seconds, etc, to dates. You can also retrieve these values.

For date formatting (the display of dates, the parsing of date strings into Date objects), I use Matt Kruse's date library from http://www.JavascriptToolbox.com/. In your example you don't really need this functionality, but in the future if you want to print dates as "December 31st, 2006" (or alternative formats) it's a great resource.

If you don't want to use existing libraries, I then suggest that you do your math on the number of milliseconds since 1970. You can get this by calling Date.getTime() where Date is a Date object. In your case, tNow.getTime(). Then to go into the future, add milliseconds. For 15 minutes, add 15*60*1000. In your for loop, you would add this amount in each iteration, and then create the new Option. Once the Date is larger than the closing time of the restaurant, you hop out of the loop.

Jon Emerson
http://www.jonemerson.net/
 
Old July 13th, 2006, 12:16 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hii Interrupt!!
******************Intervalcheck.html**************
<script>
function check()
{
var minutes
var hours
var timeclosed
counter=-1
minutes=parseInt(document.getElementById("timeinte rval").value,10)
hours=parseInt(document.getElementById("currenthou rs").value,10)
timeclosed=parseInt(document.getElementById("timec losed").value,10)
interval=minutes
minutes=parseInt(document.getElementById("currentm inutes").value,10)-interval>=0 ?(minutes*2): minutes

 for (k=hours;k<timeclosed;k++)
 {
        if(minutes==80)
        {
        minutes=interval
        }

        for(i = 1; minutes <=60; i++)
        {
            counter++
            showminutes=minutes
            document.getElementById("tSelect").options[counter] = new Option("Time="+k+"hours: minutes=" + showminutes)
            minutes+=interval
            if(minutes==60)
            {
            counter++
            minutes=80
            showminutes="00"
            document.getElementById("tSelect").options[counter] = new Option("Time="+ (k+1) +"hours: minutes=" + showminutes)
            }
         }

  }
}
</script>

Current Hour<input type=text name=currenthours id=currenthours>
<br>
Current Minutes<input type=text name=currentminutes id=currentminutes>
<br>
Time Interval<input type=text name=timeinterval id=timeinterval>(5/10/15/20/30 mints)
<br>
Time Closed<input type=text name=timeclosed id=timeclosed>(say 12 hour)

<br>
<select name=tSelect id=tSelect size=10 multiple></select>
<br>
<input type=button value=createList onclick="check()">

************************************************** ************


var tNow = new Date()
var minutes = tNow.getMinutes() + 15
var hours = tNow.getHours()


 Date function uses the client side current time.
User can set different values,in this case select list box will contain different values.
e.g 1>when user is accessing the page at 8 o'clock and
12 o'clock is the latest collection time in this case 16 15 minute slots OK!! no probs

 2>now user is changed the time by 5 o'clock then????
 in this case 28 15 minute slots,while it should be 16 15minutes slots :(

if current time is 4:10 , and time interval is 15 and time closed at 12 then next time slot should be start with 4:15 onwards

if current time is 4:20 , and time interval is 15 and time closed at 12 then next time slot should be start with 4:30 onwards

if current time is 4:15 , and time interval is 15 and time closed at 12 then next time slot should be start with 4:15 onwards

Not :-
In order to not allow current time if it is follow with time slot
e.g.
if current time is 4:15 , and time interval is 15 and time closed at 12 then next time slot should be start with 4:30 onwards then
change
minutes=parseInt(document.getElementById("currentm inutes").value,10)-interval>0 ?(minutes*2): minutes
with
minutes=parseInt(document.getElementById("currentm inutes").value,10)-interval>=0 ?(minutes*2): minutes

Hope this will help you

Cheers :)

vinod
 
Old July 13th, 2006, 08:12 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks guys thats a great help. Good links there Panacea.

Cheers
Joe

'sync' <cr>
The name specified is not recognized as an internal or external command, operable program or batch file.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Question in login script zone8567 Classic ASP Basics 1 May 25th, 2008 06:57 PM
Question on appendix B - sql script Daniel.K62 BOOK: Beginning C# 2005 Databases 1 December 30th, 2006 06:21 PM
ActiveX Script Question kwilliams VB Databases Basics 0 January 23rd, 2006 06:48 PM
action script question IP076 Flash (all versions) 1 January 10th, 2005 07:55 PM
SQL Script question... dungey PHP Databases 3 January 6th, 2005 11:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.