esthonwood 0 Posted June 9, 2013 Report Share Posted June 9, 2013 Hi guys, how can I add a user specified number of hours to the current date ($date)? For example, a user would input "6" and the script will need to add 6 hours to the current date. I have been using the datetime manipulation plugin but I can't seem to make it work. Any ideas? Thanks Quote Link to post Share on other sites
Pete 121 Posted June 9, 2013 Report Share Posted June 9, 2013 Scouce http://www.javascriptsource.com/time-date/add-time.htmlPut this in a load html, If you need it in a virable your need to pick the java apart <!-- THREE STEPS TO INSTALL ADD TIME: 1. Copy the coding into the HEAD of your HTML document 2. Add the onLoad event handler into the BODY tag 3. Put the last coding into the BODY of your HTML document --> <!-- STEP ONE: Paste this code into the HEAD of your HTML document --> <HEAD> <script type="text/javascript"> <!-- /* This script and many more are available free online at The JavaScript Source!! http://www.javascriptsource.com Created by: Sandeep Gangadharan :: http://www.sivamdesign.com/home/ */ function sivamtime() { now=new Date(); hour=now.getHours(); min=now.getMinutes(); sec=now.getSeconds(); if (min<=9) { min="0"+min; } if (sec<=9) { sec="0"+sec; } if (hour<10) { hour="0"+hour; } document.hours.clock.value = hour; document.minutes.clock.value = min; document.seconds.clock.value = sec; setTimeout("sivamtime()", 1000); } function sivamtime3() { now5=new Date(); addH = document.addhours.addclock.value; addM = document.addminutes.addclock.value; addS = document.addseconds.addclock.value; hour3 = parseInt(addH) + now5.getHours(); min3 = parseInt(addM) + now5.getMinutes(); sec3 = parseInt(addS) + now5.getSeconds(); if (sec3>=60) { sec3 = -(60 - sec3); min3 = parseInt(min3)+1; } if (min3>=60) { min3 = -(60 - min3); hour3 = parseInt(hour3)+1; } if (hour3>=24) { hour3 = -(24 - hour3); } if (sec3<=9) { sec3="0"+sec3; } if (min3<=9) { min3="0"+min3; } if (hour3<=9) { hour3="0"+hour3; } if ((addS>59) || (addM>59) || (addH>23)) { window.alert('Invalid Input!!'); return false; } if ((addS.length > 2) || (addM.length > 2) || (addH.length > 2)) { window.alert('Invalid Input!!'); return false; } if ((addS.indexOf('-') != -1) || (addM.indexOf('-') != -1) || (addH.indexOf('-') != -1)) { window.alert('Invalid Input!!'); return false; } document.hours1.clock1.value = hour3; document.minutes1.clock1.value = min3; document.seconds1.clock1.value = sec3; setTimeout("sivamtime3()", 1000); } --> </script> </HEAD> <!-- STEP TWO: Insert the onLoad event handler into your BODY tag --> <BODY onLoad="sivamtime()"> <!-- STEP THREE: Copy this code into the BODY of your HTML document --> <table border="5" align="center" cellpadding="0" cellspacing="0" width="160" bgcolor="#cccccc"> <tr> <td> <table border="0" align="center" cellpadding="0" cellspacing="0" width="80"> <tr> <td align="center" colspan="3"><br /> <font face="verdana, arial, helvetica, sans-serif" size="1"> Current Time: </font> </td> </tr> <tr> <td> <form name="hours"> <p><input type="text" size="2" name="clock" /></p> </form> </td> <td> <form name="minutes"> <p><input type="text" size="2" name="clock" /></p> </form> </td> <td> <form name="seconds"> <p><input type="text" size="2" name="clock" /></p> </form> </td> </tr> </table><br /> <table border="0" align="center" cellpadding="0" cellspacing="0" width="150"> <tr> <td align="center"> <font face="verdana, arial, helvetica, sans-serif" size="1"> Add Hours: </font> </td> <td align="center"> <font face="verdana, arial, helvetica, sans-serif" size="1"> Add Minutes: </font> </td> <td align="center"> <font face="verdana, arial, helvetica, sans-serif" size="1"> Add Seconds: </font> </td> </tr> <tr> <td align="center"> <form name="addhours"> <input type="text" size="2" value="00" name="addclock" /> </form> </td> <td align="center"> <form name="addminutes"> <input type="text" size="2" value="00" name="addclock" /> </form> </td> <td align="center"> <form name="addseconds"> <input type="text" size="2" value="00" name="addclock" /> </form> </td> </tr> <tr> <td align="center" colspan="3"> <form> <input type="button" value="Get Time" onClick="sivamtime3()" style="font-size: 9pt" /> </form> </td> </tr> </table> <table border="0" align="center" cellpadding="0" cellspacing="0" width="80"> <tr> <td align="center" colspan="3"> <font face="verdana, arial, helvetica, sans-serif" size="1"> Scheduled Time: </font> </td> </tr> <tr> <td> <form name="hours1"> <input type="text" size="2" name="clock1" /> </form> </td> <td> <form name="minutes1"> <input type="text" size="2" name="clock1" /> </form> </td> <td> <form name="seconds1"> <input type="text" size="2" name="clock1" /> </form> </td> </tr> </table> </td> </tr> </table> <p><center> <font face="arial, helvetica" size"-2">Free JavaScripts provided<br> by <a href="http://javascriptsource.com">The JavaScript Source</a></font> </center><p> <!-- Script Size: 6.07 KB --> Quote Link to post Share on other sites
UBotDev 276 Posted June 9, 2013 Report Share Posted June 9, 2013 I think you should convert $date to UNIX timestamp first and add the number of hours (converted into seconds). When those two numbers are added, convert the result back from UNIX to UBOT DATE. 1 Quote Link to post Share on other sites
esthonwood 0 Posted June 10, 2013 Author Report Share Posted June 10, 2013 I think you should convert $date to UNIX timestamp first and add the number of hours (converted into seconds). When those two numbers are added, convert the result back from UNIX to UBOT DATE. I created my own function, instead, but if mine doesn't work as expected, I will definitely try your suggestion. Thanks Quote Link to post Share on other sites
esthonwood 0 Posted June 10, 2013 Author Report Share Posted June 10, 2013 I think you should convert $date to UNIX timestamp first and add the number of hours (converted into seconds). When those two numbers are added, convert the result back from UNIX to UBOT DATE. This one seems to be the best and easiest solution than my complicated function LOL! Thank you for your help Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.