JavaScript - Jquery Date And Time Difference
Hi Guys,
I am trying to implement a countdown on my website. I want to calculate the difference between the current date and Jan 1st 2012 at 12:01am. I need to have the format in dd:hh:mm:ss so i can push it into this class. I've been reading posts but can't get it to work properly. Can anyone help me convert the dates? Thanks in advance. Similar Tutorialshi, can some one help me how to get the time and date difference? given two time and date with the following format like in textbox A: 2011-05-03 17:35:47.0 and textbox B: 2011-05-03 16:35:47.0 then the output would be: 0 days, 1 hour, 0 minutes, 0 seconds regards, verve hi, anyone knows how to get the past 6th month date using time difference? like if the current date is given 18-April-2011 then the past 6th month date is 18-October-2010...how can this be done by using time difference? Thanks, verve Hi, back for more help Im doing a hire price script for my works website, i have two time input input boxes using <select> function. I need a script to figure out the time diffrence between the two times, and if the second time is 3 or more hours before the first time it adds a days charge, well, it dosent even need to figure out the exact difference between the two, only need to see if the second is > by 3 or more, here is my script Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" profile="http://gmpg.org/xfn/11"> <head> <meta http-equiv="Content-Type" content="UTF-8" /> <title>Car Hire Ottershaw | Van Hire Ottershaw | Car Rental | Van Rental </title> <meta name="keywords" content="Car Hire Ottershaw, Surrey, Van Rental Ottershaw, Van Hire Ottershaw, Car Rental Ottershaw, " /> <link rel="stylesheet" href="layout-ff.css" type="text/css" media="screen, projection" /> <link rel="stylesheet" href="style.css" type="text/css" media="screen, projection" /> <!-- put java here................................. --> <script language="javascript" type="text/javascript" src="datetimepicker.js"> //Date Time Picker script- by TengYong Ng of http://www.rainforestnet.com //Script featured on JavaScript Kit (http://www.javascriptkit.com) //For this script, visit http://www.javascriptkit.com </script> <!-- stop java here ................................. --> </head> <body> <!-- content ................................. --> <form name="date"> <select name="car" id="car"> <option value="A">Manual Panda / Auto Smart</option> <option value="B">Manual Clio 3 3Dr or 5 Dr / Manual Picanto</option> <option value="CD">Manual Rio / Clio 1.2T / Auto Picanto</option> <option value="E">Manual Venga / Auto Rio</option> <option value="F">Auto Focus / Cee'd / Manual Megane</option> <option value="G">Auto Cee'd Estate / Manual Megane Estate</option> <option value="H">Auto Mondeo</option> <option value="I">Manual Sportage 2WD</option> <option value="J">Auto Sportage 4X4</option> <option value="K">Auto Galaxy / Manual RCZ</option> <option value="L">Auto Sorento 4X4</option> <option value="M">Manual Kangoo Van</option> <option value="O">Manual LWB LL29 Trafic</option> </select> <p>start date:</p> <input id='firstDate' type="text" size="25"><a href="javascript:NewCal('firstDate','ddmmyyyy')"><img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a> <p>Start Time: <select name="time1" id="time1"> <option value="1">08:00</option> <option value="2">08:30</option> <option value="3">09:00</option> <option value="4">09:30</option> <option value="5">10:00</option> <option value="6">10:30</option> <option value="7">11:00</option> <option value="8">11:30</option> <option value="9">12:00</option> <option value="10">12:30</option> <option value="11">01:00</option> <option value="12">01:30</option> <option value="13">02:00</option> <option value="14">02:30</option> <option value="15">03:00</option> <option value="16">03:30</option> <option value="17">04:00</option> <option value="18">04:30</option> <option value="19">05:00</option> <option value="20">05:30</option> <option value="21">06:00</option> <option value="22">06:30</option> <option value="23">07:00</option> <option value="24">07:30</option> </select> <p>end date:</p> <input id='secondDate' type="text" size="25"><a href="javascript:NewCal('secondDate','ddmmyyyy')"><img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a> <p>End Time: <select name="time2" id="time2"> <option value="1">08:00</option> <option value="2">08:30</option> <option value="3">09:00</option> <option value="4">09:30</option> <option value="5">10:00</option> <option value="6">10:30</option> <option value="7">11:00</option> <option value="8">11:30</option> <option value="9">12:00</option> <option value="10">12:30</option> <option value="11">01:00</option> <option value="12">01:30</option> <option value="13">02:00</option> <option value="14">02:30</option> <option value="15">03:00</option> <option value="16">03:30</option> <option value="17">04:00</option> <option value="18">04:30</option> <option value="19">05:00</option> <option value="20">05:30</option> <option value="21">06:00</option> <option value="22">06:30</option> <option value="23">07:00</option> <option value="24">07:30</option> </select> <p>Days:</p> <input type="text" name="days" /> <p>Price:</p> <input type="text" name="rentalamount" /> <p>Deposit:</p> <input type="text" id="depo" name="depo" /> <p>Excess mile price</p> <input type="text" name="mile" /> <p><input type="button" value="Quote" onclick="dateDiff() ;calculate() ;deposit() ;milecharge() ;agecheck()" /> <INPUT TYPE=RESET name="clear" value="Clear Form"/></p> </form> <p> GAV: Deposit and mile excess now working, 28 days now works and now have a reset button also made a pop up for age restriction(select sportage/galaxy/rcz ) all prices are now on here now all to do is set up cecks on the dates ( summer/ winter. collect before return e.c.t) im starting to like this java script **** !</p> <!-- /content --> </body> </html> thanks Please can u help? how would i get the total work hours between two times (8:30 to 5:30) from a web form. I tried just minusing one from the orther but it came back with "Nan" I've written a js function to find the difference between two dates. the format being used is dd/mm/yyyy hh:mm. The function returns correct value when give one set of values, but go wrong with another set. examples are given below. set 1 : Time 1 = 24/02/2011 09:30 , time 2 = 24/02/2011 16:00 Output is corret here. It gives 6 Hours & 30 Minutes (after converting the difference) set 2: Time 1 = 24/02/2011 09:30 , time 2 = 25/02/2011 16:00 Here, it gives 31 days, 6 Hours & 30 Minutes. My code is given below. Also the alert of dates display strange values. Don't know if it is timezone issue. I wonder what is going wrong here Code: function compareDateTime(frmtime,totime) { var date1 = new Date(frmtime); var date2 = new Date(totime); var diff = new Date(); alert(date1); alert(date2); diff = (date2.getTime()-date1.getTime()); if (diff<0) { alert("From Time cannot be later than To Time!"); return 0; } else if (diff==0) { alert("From Time cannot be equal with To Time!"); return 0; } else { return diff; } } The returned diff value is broken down as following: Code: if (diff>0) { days = Math.floor(diff / (1000 * 60 * 60 * 24)); diff -= days * (1000 * 60 * 60 * 24); hours = Math.floor(diff / (1000 * 60 * 60)); diff -= hours * (1000 * 60 * 60); mins = Math.floor(diff / (1000 * 60)); alert(days+","+hours+","+mins); return true; } what am i doing wrong here? i've written a js function to find the difference between two dates. the format being used is dd/mm/yyyy hh:mm. The function returns correct value when give one set of values, but go wrong with another set. examples are given below. set 1 : Time 1 = 24/02/2011 09:30 , time 2 = 24/02/2011 16:00 Output is corret here. It gives 6 Hours & 30 Minutes (after converting the difference) set 2: Time 1 = 24/02/2011 09:30 , time 2 = 25/02/2011 16:00 Here, it gives 31 days, 6 Hours & 30 Minutes. My code is given below. Also the alert of dates display strange values. Don't know if it is timezone issue. I wonder what is going wrong here Code: function compareDateTime(frmtime,totime) { var date1 = new Date(frmtime); var date2 = new Date(totime); var diff = new Date(); alert(date1); alert(date2); diff = (date2.getTime()-date1.getTime()); if (diff<0) { alert("From Time cannot be later than To Time!"); return 0; } else if (diff==0) { alert("From Time cannot be equal with To Time!"); return 0; } else { return diff; } } The returned diff value is broken down as following: Code: if (diff>0) { days = Math.floor(diff / (1000 * 60 * 60 * 24)); diff -= days * (1000 * 60 * 60 * 24); hours = Math.floor(diff / (1000 * 60 * 60)); diff -= hours * (1000 * 60 * 60); mins = Math.floor(diff / (1000 * 60)); alert(days+","+hours+","+mins); return true; } Please Help... Hi All, I have a form that captures member registration details. In the admin section .. I would like to change the Status field of a record/s from NEW to PAYMENT DUE after 14 days from the data of submission. The status change should automatically trigger on the 15th day. So when the admin checks the list page he/she should be able to view the updated status field. Any pointers how to accomplish this? Thanks in advance. Vinny Good day! I explore something so I used jquery in my date, without any knowledge about jquery. I research a sample Jquery Date Picker internet and I found one, my problem is the Year is started at 2001 , I don't know if I could change it so that the year would start atleast 1950's or 1990's here's the code that I add in my webpage to display the Jquery Date Picker Code: <link rel="stylesheet" href="ui.all.css" type="text/css" media="screen" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.5.3/jquery-ui.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#date").datepicker({ showOn: 'button', buttonText: "Select your Birthday" }); }); </script> I will attach the date picker that I get from internet. Thank you Hi, I am using the JQuery UI Datepicker range. I would like to change my dates from: mm/dd/yy to: dd/mm/yy Can someone please help me?! thanks Code: var dates = $( "#from, #to" ).datepicker({ defaultDate: "+1w", changeMonth: true, numberOfMonths: 3, onSelect: function( selectedDate ) { var option = this.id == "from" ? "minDate" : "maxDate", instance = $( this ).data( "datepicker" ), date = $.datepicker.parseDate( instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings ); dates.not( this ).datepicker( "option", option, date ); } }); Hi. I am having a little trouble with the following code, I am unsure where the problem is I will start from the beginning. Let me know if this is correct. 1. asked to declare a variable named timeStr=to the value returned from the showDateTime() function. 2.declare a variable named mapNum equal to the value returned from the getMap() function. Code: <title>SkyWeb: The Planisphere</title> <link href="skyweb.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="datetime.js"></script> <script type="text/javascript"> function timestr(){ /* timestr is a text string containing the current date and time mapNum is the number of the map to display in the planisphere */ var timeStr=showDateTime(); var mapNum=getMap(); } </script> </head> It asks me to scroll down to the div element with the id value "maps" and replace the line <img id="sky" src="sky0.jpg" alt=""/> with a script element that writes the following code<img id='sky' src='skymapNum.jpg' alt='' /> where mapNum is the value of the mapNum variable. replace the date/time value "Jan 1 2007, 12:00 am" with a script element that writes the value of the timeStr variable to the web page. Code: <div id="maps"> <img id="sky" src="sky0.jpg" alt="" /> <img id="mask" src="mask.gif" alt="" /> <div id="datetime"> January 1, 2007, 12:00 am </div> </div> I have tried it several ways but have been unable to get this through my think head, I am new to JS the textbook isnt very explanitory as you guys have been. Thanks for your time. Hi guys, I have JavaScript code that displays the date, time and URL in the web page, here is the code: Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>JavaScript Page One</title> </head> <body> <script type="text/javascript"> var today=new Date() var browser=navigator.appName; var b_version=navigator.appVersion; var version=parseFloat(b_version); document.write(''+today.toString()+'<br>'+window.location+'') document.write("<br />"); document.write("Browser name: "+ browser); document.write("<br />"); document.write("Browser version: "+ version); </script> </body> </html> How can i format it so that it uses the following colors for different date and time components: Day in red, Date in green, Month in yellow, Year in blue, Hours and Minutes in brown? And also, what is the JavaScript code to successfully display the number of forms, anchors and links in the page? Thanks ~Savage Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <script type="text/javascript"> var info = new Date() var day = info.getDate() var mon = info.getMonth() var year = info.getFullYear() var hour = info.getHours() var min = info.getMinutes() var sec = info.getSeconds() alert(day+","+mon+","+year+","+hour+","+min+","+sec+",") if (day>=22 && mon>=5 && year>=2011 && hour>=12 && min>=00 && sec>=00) { document.write{ <script src="//c.tadst.com/common/fullscreen2.js" type="text/javascript"></script><tr><td id="c1" align="center" valign="middle"><h2 align="center" class=c>Time <span id=el_u1>until</span> SUMMER HOLIDAYS </h2> <div align="center"> <table class=wa> <tbody id=rs3><tr><td><span id=el_d1>7</span> <span id=el_d1t>days</span>, <span id=el_h1>19</span> <span id=el_h1t>hours</span>, <span id=el_m1>58</span> <span id=el_m1t>minutes</span>, <span id=el_s1>18</span> <span id=el_s1t>seconds</span></td></tr></tbody></table> </div> <h2 align="center"><span class="c">that is</span></h2> <p align="center"><script type="text/javascript"> <!-- et=1308231902;function f0(d){return ld[d.getUTCDay()]+','+' '+d.getUTCDate()+'.'+' '+lm[d.getUTCMonth()]+' '+pf(d.getUTCFullYear(),4)+', '+p2(d.getUTCHours())+':'+p2(d.getUTCMinutes())+':'+p2(d.getUTCSeconds());} cks={el:{t:[{o:7200,a:'CEST',d:1}],f:f0,c:{t:1308736800E3}}}; lm=new Array();lm[5]='Juni';ld=new Array();ld[2]='Dienstag';ld[3]='Mittwoch';ld[4]='Donnerstag';//--> </script> </p> <div align="center"> <table class="wa"> <tbody id="rs1"> <tr> <td id="el_d2" class="r dbl">7</td> <td class="dbl"> </td> <td id="el_d2t" class="dbl">days</td> </tr> <tr> <td id="el_h2" class="r dbl">187</td> <td class="dbl"> </td> <td id="el_h2t" class="dbl">hours</td> </tr> <tr> <td id="el_m2" class="r dbl">11275</td> <td class="dbl"> </td> <td id="el_m2t" class="dbl">minutes</td> </tr> <tr> <td id="el_s2" class="r dbl">676517</td> <td class="dbl"> </td> <td id="el_s2t" class="dbl">seconds</td> </tr> </tbody> </table> <script src="//c.tadst.com/common/common21.js?5" type="text/javascript"></script> <script src="//c.tadst.com/common/wcupd8.js?1" type="text/javascript"></script> </div> }; } else { document.write{ <img src="happysummerholiday.jpg" width="285" height="200" /> }; } </script> </body> </html> The Red part is html coding. Doesn't seam to work - please help me. The idea is that after the date (22.06.2011 at 12:00) the content changes to the picture. Thank you in advanced for helping me I have created a kdate.js file in Kannada to display current, day,month,date,year and time.While the day,month are displayed in kannada the date,year time are shown in english.I want them to display in Kannada.Will some one guide me how I can do this and what are changes that to make in the javascript code.I have used unicode while writing Kannada script. I also want to know how to increase and change font color and the time element to be displayed on the next line. Please help and guide me. thanks js file in the form the text is sent as an attachment. Hi, I am using jQuery datepicker's date range, I have a dynamic form where date range fields are added dynamically, for this reason I have set the two selectors for jQuery date range to classes. Below is my code: Code: $( ".datepicker_start_date, .datepicker_end_date" ).livequery(function() { var dates = $( ".datepicker_start_date, .datepicker_end_date" ).datepicker({ defaultDate: "+1w", changeMonth: false, dateFormat: 'dd-mm-yy', numberOfMonths: 1, onSelect: function( selectedDate ) { var getclass = $(this).attr("class"); var option = getclass.indexOf("datepicker_start_date") != -1 ? "minDate" : "maxDate", instance = $(this ).data( "datepicker" ), date = $.datepicker.parseDate( instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings ); dates.not( this ).datepicker( "option", option, date ); } }); }); The above works perfect if I have just two field to range the date such as fields with class .datepicker_start_date and .datepicker_end_date. But if I have multiple fields like have 3-4 more date range fields, it will just mess the dates and add dates of field1 to field 3 and vice versa. I would appreciate if someone could help. Many Thanks I have the following code on my site. I use this in the header of mysite Code: <script type="text/javascript"> var weekdaystxt=["Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"] var montharray=("January","February","March","April","May","June","July","August","September","October","November","December") function showLocalgetthedate(){ var mydate=new Date() var year=mydate.getYear() if (year < 1000) year+=1900 var day=mydate.getDay() var month=mydate.getMonth() var daym=mydate.getDate() if (daym<10) daym="0"+daym function showLocalTime(container, servermode, offsetMinutes, displayversion){ if (!document.getElementById || !document.getElementById(container)) return this.container=document.getElementById(container) this.displayversion=displayversion var servertimestring=(servermode=="server-php")? '<? print date("F d, Y H:i:s", time())?>' : (servermode=="server-ssi")? '<!--#config timefmt="%B %d, %Y %H:%M:%S"--><!--#echo var="DATE_LOCAL" -->' : '<%= Now() %>' this.localtime=this.serverdate=new Date(servertimestring) this.localtime.setTime(this.serverdate.getTime()+offsetMinutes*60*1000) //add user offset to server time this.updateTime() this.updateContainer() } showLocalTime.prototype.updateTime=function(){ var thisobj=this this.localtime.setSeconds(this.localtime.getSeconds()+1) setTimeout(function(){thisobj.updateTime()}, 1000) //update time every second } showLocalTime.prototype.updateContainer=function(){ var thisobj=this if (this.displayversion=="long") this.container.innerHTML=this.localtime.toLocaleString() else{ var hour=this.localtime.getHours() var minutes=this.localtime.getMinutes() var seconds=this.localtime.getSeconds() var ampm=(hour>=12)? "PM" : "AM" var dayofweek=weekdaystxt[this.localtime.getDay()] this.container.innerHTML=formatField(hour, 1)+":"+formatField(minutes)+":"+formatField(seconds)+" "+ampm+ " "+montharray(month)+" ("+dayofweek+")" } setTimeout(function(){thisobj.updateContainer()}, 1000) //update container every second } function formatField(num, isHour){ </script> I use this to display the information. Code: <script type="text/javascript"> new showLocalTime("timecontainer", "server-php", 0, "short") new showLocalTime("timecontainer2", "server-php", +300, "short") new showLocalTime("timecontainer3", "server-php", +360, "short") new showLocalTime("timecontainer4", "server-php", +600, "short") </script> This gives me 4 clocks which i set at different time zones Can someone assist me in showing me a way to accelerate the time for this clock. I would like the minutes to be only 5 seconds long versus the normal 60 seconds per minute. I fly a flight sim and the in game world clock is accelerated at this pace and my squad would like to be able to see the time it would currently be in the game so we can avoid launching the game at certain in game times. If this can't be done, does anyone know of a separate java script that can accomplish this? Thank you in advance, any and all help would be greatly appreciated. Hello. I just want to check whether it is possible (and if so whether there is one available) to have a script that displays MY current time and date to visitors to my website. My time is Western European Time or Greenwich Mean Time. I would like visitors from eg Japan to see my local time and not their own. I think some date scripts I have seen use the user's local PC clock, but I am not sure? Thanks for any feedback and help! Quote: <SCRIPT LANGUAGE="JavaScript1.2"> <!-- Begin var days=new Array(8); days[1]="Monday"; days[2]="Tuesday"; days[3]="Wednesday"; days[4]="Thursday"; days[5]="Friday"; days[6]="Saturday"; days[7]="Sunday"; var months=new Array(13); months[1]="January"; months[2]="February"; months[3]="March"; months[4]="April"; months[5]="May"; months[6]="June"; months[7]="July"; months[8]="August"; months[9]="September"; months[10]="October"; months[11]="November"; months[12]="December"; var time=new Date(); var lmonth=months[time.getMonth() + 1]; var ldays=days[time.getDay() + 1]; var date=time.getDate(); var year=time.getYear(); var hour=time.getHours(); var min=time.getMinutes() if (year < 2000) year = year + 1900; if (hour > 15) { date = date + 1; } document.write("<center>" + lmonth +" "); document.write(date + ", " + year + "</center>"); // End --> </SCRIPT> </CENTER> Can anyone help me edit this so that on Saturday at 10am the date displays mondays date. Thanks Hi All, I wonder if you can help me with some time and date formatting in JS. I already use a neat date formatting JS script which is useful when a PERL script returns a date field of 'DATE' by converting it from yyyy-mm-dd to dd month(in text) yyyy: <script type = "text/javascript"> var months = ["January","February","March","April","May","June","July","August","September","October","November"," December"]; var myString = "[[date]]"; var mySplitResult = myString.split("-"); var mm = parseInt(mySplitResult[1],10)-1; // note that months are 0-11 document.write(mySplitResult[2] + " " + months[mm] + " " + mySplitResult[0] ); </script> I have, however, run across a little problem where I have results coming through with date AND time, e.g. 18:12 2010-07-14 What I need is something that will reorder the above to: 14 July 2010 at 18:12 Can someone help please! Regards, Neil Hello all. I have created a 2 frame site where I want the top frame (header) to redirect the other frame (main) to a different page depending on the date, time, and second. For example, on 12 FEB at 07:00:00, I would like the header frame to redirect the main frames page to EmmasBday.html; in which I would be alerted of my little sisters Birthday. This is for personal use and isn't going to be hosted online so I will just use JavaScript, it's basically a simple system to remind me of crucial events at certain times and dates so I don't forget them. The same events will apply every year, so there is no need to programme in specific years although having the option could prove useful. I am aware of other alternatives to achieve this, but would very much like to work with JavaScript; as I can expand it. As I state, this is for personal use and therefore won't be hosting it; I will use JavaScript throughout and cannot use anything else like PHP, Perl, etc. I am aware JavaScript takes the time & date from the computer hosting it (unlike PHP), which is fine. So, for example; at 14:30:00 I will be informed to get dinner; and at 15:00:45 I'll be informed to study. The knowledge you guys have presented so far is phenomenal, and I couldn't think of anybody better to ask for assistance. Anyway, I hope you can resolve my problem and I'm looking forward to a response! Thanks for your concern, you brain Gods you! AHa. |