JavaScript Date Difference, Accounting For Leap Years
Jun 19, 2006
I'm really sorry to post this as I know it must have been asked
countless times before, but I can't find an answer anywhere.
Does anyone have a snippet of JavaScript code I could borrow which
calculated the difference in years and days between two dates, and
takes leap years into account?
I'm calculating the difference in the usual way, i.e....
var difference = dateTo.getTime() - dateFrom.getTime();
....and converting this millisecond value into days by using...
var daysDifference = (difference/1000/60/60/24);
But how do I then display the difference in days AND years? I've tried
the following:
var yearsDifference = Math.floor(daysDifference/365.25);
var daysLeft = Math.floor(daysDifference-(yearsDifference*365.25));
....but it gives me inaccuracies. For example, if I use my code to
calculate the difference between 05/01/1998 and 05/01/2000 it returns 1
year and 364 days!
If you see my last post, it was a piece of this code that I was learning for/loops but now I have that all working(hence the new topic) and I'm having a problem with indexOf. Here's the whole script(its a fun little date picker)
function popSel(v){ var days; var x; if(v==01 || v==03 || v==05 || v==07 || v==08 || v==10 || v==12){ x = 31; }else if(v==04 || v==06 || v==09 || v==12){ x = 30; }else if(v==02){ /*var yr=document.getElementById("year").value / 4;*/ var yr = 2003 / 4; if(yr.indexOf(".") == -1){ /*If you divide a year by 4 and the value is a whole number, it's a leap year*/ x=28; }else{ x=29; }}for(i = 1; i <= x; i++){ days += "<option value=" + i + ">" + i + "</option>"; }document.getElementById("day").innerHTML=days; } The whole script works but feb doesn't change?
I am having some trouble with my code in that I am trying to take the value from the date of birth field and check to see if over 18 years old. I they are not over 18, a window.alert dialog box pops up to tell them. Below is the check if 18 validation.
Script section in the document head:
//check if over 18 function overAge(){ var age;
[code]....
Code for body section:
Date of Birth: <br/> <input type = "text" name = "date" value = "(mm/dd/yyyy)"/>
how to calculated the difference between two date feild(inputed by user using datepicker) format (yyyy-mm-dd).and store in another feild. As i tried to do but not able to get it Attached File(s)
test.php (1.36K) Number of downloads: 89 t2.php (1.3K) Number of downloads: 92 s1.php (1.58K) Number of downloads: 94
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.
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?
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.
What is the best way to get the position of an element relative to the document (accounting for margins as well)?I would like to position another element over it. I'm using the offset() function but i am running into issues 1. The values for top & left returned in IE 8 (perhaps other versions) are incorrect.2. Offset() doesn't take into account margins of the element. And when margins are set to auto i cannot seem to get the correct margin values to use
I am looking for a generic javascript function to identify the form with in which a element exists, thus developer can avoid coding like document.forms[index].submit() - where they are sure, they want the form with in which this element exists is to be submitted. This way when forms are introduced at top level the code can remain unchanged, else every form introduce above in the DOM will result in increasing the index by one.
The code is given below. While doing so i am stuck with 2 problems P1: In the code, you can see i am giving a explicit 'break' in the code. If i don't do so the code seems to be looping. Obviously i am missing some basic.
P2: I am able to pass 'this' for anchor element by name="xy" <a href="#" name="xy"
in its onclick function by which, in the javascript function i move up the DOM.
While i am unable to do that on the anchor element given below since this function is now on the href attribute.
how the class attribute works in CSS: I can specify that every attribute with class="foo" be italicized with: .foo {font-style: italic;}
What if I want to do the same sort of thing in Javascript? Is there something built-in like document.GetElementsbyClass("foo").style = "italic" (I'm just making this code up of the top of my head, I don't know how javascript works.)
It seems to me that with JavaScript you're supposed to use the name="" attribute instead of class="". Why the diference?
I looked at webmonkey that had an article about this, but their writing style is simply confusing and I can't make heads or tails of their articles.
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
I have dropdown for month and work days(in a month),textbox for year(yyyy).
I need to validate, if user select febraury and select other than 28 ( for non leap year), and select other than 29 ( for leap year) user should get error message.
same validations should be done for other months also.I need in Javascript or C#(VS2005)
A non leap year can be divided by 100. But a leap year is divisible by both 4 and 400.This seems so simple yet I can't nail it. I'm taking an accelerated Javascript class and I was able to do all the problems up to this one. It's confusing me, or it may be that I've been up since 2 working on homework and getting ready for another test.
Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html>
I have to code something where the input would be the age (in years) and the output would be the age (in days). I'm not sure what I did wrong, but it's not working,
I'm trying to get a pair of drop down menus to appear on the screen, one containing months, and one containing years. I know the one containing years works, since it hasn't been changed at all since it was working, but the month box used to be a textbox. I tried to convert it over to drop down box using the same code as the year box, but it hasn't seemed to work.
<script language="Javascript"> var m1 = January; var m2 = February; var m3 = March; var m4 = April; var m5 = May; var m6 = June; var m7 = July; var m8 = August; var m9 = September; var m10 = October; var m11 = November; var m12 = December; .....
document.write('<select name="selYear">'); for (var x=0; x <= 10; x++ ) { if(x != 5){ document.write('<option value="' + x + '">' + yearArray[x] + '</option>'); }else{ document.write('<option value="' + x + '" selected>' + yearArray[x] + '</option>'); }} document.write('</select>'); </SCRIPT>
I am trying to create a drop down menu for dates that starts with today's date, but allows people to choose 50 years into the past and 10 years into the future.I am also having a problem with the month. I wanted it to show the name of the month, but I am getting a number (and a wrong one at that).
I'm looking for some Javascript that displays the current year and the next 10 years afterwards as options in a select menu, and that auto updates as the years pass.
Recently while messing with dates, I noticed an odd quirk in javascript with new Date(), i.e. if someone enters an invalid date, such as 2/29/2003, javascript creates the new date as 3/1/2003.
Having a look around, I couldn't find any scripts that took advantage of this for the sake of date validation... probably someone here has done this before, but I'll post it anyway.
The idea is that if javascript creates a new Date() with a different month, then obviously the date entered is not valid. Most of the scripts I saw used some math to divide by leap year, yadda yadda yadda, but with this feature (?) of javascript, it seems unnecessary.
Right now this code only validates mm/dd/yyyy, but it should be easy to modify to support other formats:
function isDate(sDate) { var re = /^d{1,2}/d{1,2}/d{4}$/ if (re.test(sDate)) { var dArr = sDate.split("/"); var d = new Date(sDate); return d.getMonth() + 1 == dArr[0] && d.getDate() == dArr[1] && d.getFullYear() == dArr[2]; } else { return false; } }
Here's a shorter version that works if you pass in the values separately:
function isDate(mm,dd,yyyy) { var d = new Date(mm + "/" + dd + "/" + yyyy); return d.getMonth() + 1 == mm && d.getDate() == dd && d.getFullYear() == yyyy; }
I have written a javascript to compare current date with the date that user has entered in the form. the user enters in YYYY-MM-DD format. here'z the code:
var today= new Date(); var stDate= new Date(document.form[0].startDate.value); var day=0; var month=0; var year=0; var todayStr; day= today.getDate(); month= today.getMonth()+1; year= today.getFullYear(); todayStr= new Date(year + "-" + month + "-" + day); alert(todayStr); if(todayStr>stDate) { alert("Current date is greater");}
but todayStr gives NaN . and comparison gives no result.