# 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!

## Making February Have A 29th Day To Choose From On Leap Years Only?

Apr 7, 2009

How do you think I should go about making February have a 29th day to choose from on leap years only? Years that are multiples of 4 that is...

Code:
init();
function init(){

[code]....

## IndexOf - Leap Year In Date Picker

Jan 7, 2012

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?

## JQuery :: List Years Back To A Certain Date?

Jun 3, 2010

I want to basically create an array of years from the current year back to a specified year.

## Taking Date From Form Field And Checking If Over 18 Years Old?

Dec 8, 2010

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)"/>

## Getting The Date Difference Between 2 Dates?

May 25, 2011

How do I get the difference in days between 2 dates using Javascript?

## Difference Between Date Fields?

Aug 31, 2010

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)
t2.php (1.3K)
s1.php (1.58K)

## Jquery :: Date And Time Difference?

Oct 30, 2011

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.

## Date Difference In 3rd Input Box In Days?

Apr 7, 2011

I want to make a form , in which I have 4 input Fields.

Date1
Date2
Calender Days
Working Days

When I enter date1 as 2011-04-07 and date2 as 2011-04-11

Then I require in Calender Days = 5 inclusive of date1 and date2

and Working days = 3 as SAT, SUN Off

## How Many Seconds In The Difference Between 2 Date Times?

Jun 2, 2011

I'm writing a little script thats a countdown timer.What i want to do is be able to set a length of time like 3, 5, 7 days etc.So i will have

var dif = 3;
var now = new Date();
var end = new Date(now.getFullYear(), now.getMonth(), now.getDay() + dif);

how do I find how many seconds make up the difference between the two dates and then how do I count down from now to the end time.

## Get The Past 6th Month Date Using Time Difference?

Apr 18, 2011

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?

## Change The Status Field Based On Date Difference?

May 28, 2010

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.

## Text/javascript, Application/x-javascript, Difference?

Feb 19, 2007

two possibilities or the attribute type of script:

text/javascript (the one i usually use) application/x-javascript

what are the differencies between both?

depends on the html content?

for example html 4.0.1 versus xhtml 1.1?

## JQuery :: Get The Position Of An Element Relative To The Document (accounting For Margins As Well)?

May 26, 2010

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

## Difference In Calling Javascript In Href And Onclick Of Anchor Tag.

Jan 18, 2006

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.

<a name="xy1" href="javascript:doFormSubmit(this);">Q test</a>

Hence the script fails. Code:

## Difference Between Class And Name Properties In HTML, As Applied To JavaScript

Dec 17, 2001

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?

## Getting The Time Difference Based On Date And Time

May 8, 2011

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

## Leap Year Validation-urgent

Jun 8, 2007

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)

## Form Field To Check For A Leap Year?

May 3, 2010

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>

[code]....

## Age In Years To Age In Days Calculator?

Apr 14, 2011

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,

[Code]...

## Calendar - Dropdowns Containing Months And Years

Apr 7, 2009

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>

## Drop Down Menu For Years Past And Future

Dec 29, 2010

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).

## Current And Future Years In A Dropdown Menu?

Jun 8, 2011

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.

## JQuery :: Changing Datepicker's Date Format Of Selected Date From Date Picker

Mar 16, 2011

I am having difficulty trying to change the format of selected date from date picker. This is a test so my code is very simple. Here it is.

[Code]....

## JavaScript Date Validation

Feb 8, 2003

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;
}

## Problem With Javascript Date()

Nov 15, 2005

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);