JavaScript - Difference In Dates
Hi there
How can you calculate the difference in two dates that are in different formats? I have one date as: '2010-06-16 21:20:44.000' and the other date as now = new Date(); I want to subtract the dates to see if it is over 30 days. Thanks! Similar TutorialsCalculating the difference between two dates in JavaScript is relatively straightforward, provided you choose the right Date methods to work with. Whichever way you get there.... see this http://www.tutorials99.com/tutorials...aScript/page_1 Hello, I'd like to be able to calculate the difference between 2 dates in the format dd/mm/yyyy var date1 = 06/01/2012 var date2 = 10/01/2012 Then an alert box would tell me there is a difference of 4 days. Thanks a bunch Jay. Hi so because the jquery range slider has no date range ability and because the only date range plugin for jquery UI sliders is very bloated and fails to work on modern ipad / new touch event methods... I'm writing my own today.. Can some one better exmplain to me how to do: "theCurrentDate (05/12/2012) - anOlderDate (05/12/1990) = difference of how many days is?" and "theCurrentDate (05/12/2012) - 200 = but resulting in date formatted version, not some 9394829398 number"? Thanks! I'm trying to write something to display the percentage difference of my parents' ages. I'm obviously having issue. Also I would like to shorted the percentage output to three digits, e.g. 9.25%. Any help would be greatly appreciated. Here is what I have. Code: //Set the two dates var dadDate=new Date(1964, 9, 27); //Month is 0-11 in JavaScript var momDate=new Date(1962, 4, 29); var today=new Date(); //Get 1 day in milliseconds var one_day=1000*60*60*24; //Calculate difference btw the two dates, and convert to days var dadDay=Math.ceil(today.getTime()-dadDate.getTime())/(one_day); var momDay=Math.ceil(today.getTime()-momDate.getTime())/(one_day); //Calculate difference var abDif=Math.abs(dadDay-momDay); var agePer=Math.ceil(((abDif)/(dadDay + momDay)/2)*100); document.write"Dad, congratulations. You are " agePer "% younger than Mom."; I am trying to launch an HTML page with a calendar field populated with today's date. I have a function to set the date but I am getting an error when I try to run the page: Code: //Begin function window_onload function window_onload() { UpdateData(); // TODO: Add your event handler code here function onInitialUpdate() { document.getElementById('calendar1').value=getTodaysDate(); } } //End function window_onload function getTodaysDate(){ var today = new Date(); var dd = today.getDate(); var mm = today.getMonth()+1; var yyyy = today.getFullYear(); if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} var today = yyyy+'/'mm+'/'+dd; alert('Today is '+mm+'/'+dd+'/'+yyyy); return today; } gives me an error that the onload function is expecting an object. Is ther something wrong with my coding? 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 Can anyone explain why this isn't working!? I hate JavaScript and dates! For the date: 07/08/2010 it will output: 6/7/2010. Thanks for any help. Code: var startDay = startDate.value.substr(0,2); var startMonth = startDate.value.substr(3,2); var startYear = startDate.value.substr(6,4); var myDate = new Date(startYear,startMonth-1,startDay+numberNights.value); endDate.value = myDate.getDay() + '/' + myDate.getMonth() + '/' + myDate.getFullYear(); I'm not sure if java is the best way to go with this, I'm pretty inexperienced with java, but I have a workable knowledge of html, and couldn't see a way to do it that way... so here goes: For my job I work with a ton of different deadlines all the time. What I want to do is to make a webpage that displays the date N days ago, and N days into the future. So something like 7 days ago, 30 days ago, 60 days ago 75 days ago, 30 days from now, 60 days from now, 75 days from now. etc. I've found brief snippets for how to display today's date, but i'm having trouble with the calculations. Then I also need one that displays 18 years ago, I'm not sure if the change from days to years makes any kind of difference, but I suppose it's easy enough to convert years to days anyway.... How would I go about doing that? Thank you!!! Hi peeps, I thought I'd post this here as I haven't had any luck finding what I'm after but wondered it someone else had seen a slideshow that worked this way. In theory it seems simple enough to do. Description: Bascially I'd have a slideshow with several list items (or divs) within it. Now the main think with this slidshow is the list items or divs would have differing widths. The pagination would be 'Previous' and 'next' arrows. When you clicked next, it would slide along and line up the left edge of the next item/div with the left item of the slideshow. An likewise for every click. Esentially I guess you'd be sliding along the width of the 'active slide'. 2 items/divs being visible in the window is not a problem, neither is one being cropped off at the end. As the 'active' slide would always be the left most slide. Does this seem Ok, am I making any sense at all? I hope so! If anyone has seen something like this it would be great or maybe point me in the right direction. Cheers & thanks for reading, Ste is there a simple way or any way at all of making the ($50.00) ($40.00) ($30.00) ($20.00) show the difference instead... like if i chose $40.00 then the $50.00 would say +$10.00 and the 30 and 20 would say -$10.00 and -$20.00 Code: <p align="left">CASE</p> <center> <select name="caseselect" style="width:625px" onchange="Calculate(); swapImage(this,'caseimg');"> <option value="50#COOLER MASTER Elite 310red.jpg" selected="1">COOLER MASTER Elite 310 Red ($50.00)</option> <option value="40#COOLER MASTER Elite 310blue.jpg">COOLER MASTER Elite 310 Blue ($40.00)</option> <option value="30#COOLER MASTER Elite 310orange.jpg">COOLER MASTER Elite 310 Orange ($30.00)</option> <option value="20#COOLER MASTER Elite 310silver.jpg">COOLER MASTER Elite 310 Silver ($20.00)</option> </select> Hi all, I use JS to dynamically generate HTML such as a <select> dropdown menu. I do it by placing an empty element where I want the dropdown to appear (i.e. <span id="dropdown"></span>) then getElementById('dropdown') and insert the HTML with innerHTML = html. But I could also do it by getting the empty element and then doing an "insertBefore" or "appendChild" call. Is one "preferred" over the other? Is one "better" than the other? Thanks. -- Roger 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 have 2 dates in variables (formatted like 'Wed Jan 18 2012 01:01:01 GMT+0000 (BST)') I need to minus them to get the days This worked if they were in the same month var amountOfDays = date.getDate() - datefirst.getDate(); However if the dates where in 2 different months it doesnt work. How would i do it better 2 months? This is my problem: I have a seperate page on my website with a list of upcoming events. On that page I have the date of the upcoming event and then the description. On the main page of my website, I have a small table with the next four upcoming events(with the dates of course). So far I have been manually changing the four closest upcoming events on the main page. (just copy and paste from the upcoming events page) My question: Can javascript "grab" from the upcoming events page the next four events, and have them automatically pasted into the table on my main page? And if it is possible, what code(s) do I need? This way I don't have to do it manually and will save me lots of time. P.S. I have a weak understanding of javascript, but throw any code at me and I will try to understand it. Thank you Hi I have the following script Code: function timeDifference(laterdate,earlierdate) { var difference = laterdate.getTime() - earlierdate.getTime(); var daysDifference = Math.floor(difference/1000/60/60/24); difference -= daysDifference*1000*60*60*24 document.write('difference = ' + daysDifference + ' day/s '); } var laterdate = ? ; var earlierdate = ? ; timeDifference(laterdate,earlierdate); //--></script> and would like to insert my datein and dateout values where the ? marks are above in red. I can call these variables into the form on the page using <%=Request("datein")%> but how do I call a variable into the script above ? Thanks Hi I'm relatively new to Javascript and I'm trying to learn more about dates my problem is as follows I have to do validation on a form for 2 dates(StartDate and EndDate) the dates cannot be more than four months apart if they are an alert must be displayed. In other words Nov 2009 till March 2010 or May 2009 till September 2009 should be invalid regardless of the day of the month Simply 2 input boxes(StartDate and EndDate) and a submit I've got the other parts of my page worked out just this has me stumped I would appreciate any help in this regard Thank You for your time I really don't understand JavaScript... xD Code: var inputDate = "<?php echo $inputDate2; ?>"; var JDates = new Array(); Jdates = inputDate.split("/"); var currentDate = new Date(parseInt(Jdates[2]), parseInt(Jdates[0])-1, parseInt(Jdates[1])); So... when creating 'currentDate' Jdates[0] will equal 0, even though I just asked javascript to split the inputDate by all "/". The inputDate var equals "08/06/2010" before the split. Any help greatly appreciated! Hi Guys I have created a flight/hotel itinerary and I need to work out the duration of the customers' holiday in order to work out how much it will cost to charge that person per night at their selected hotel. The problem I have is that the dates are going to vary due to user inputs in which I have used jquery 's " datepicker " Cheers George 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. |