# Problems with leap year in date picker

Page 1 of 1

## 5 Replies - 5793 Views - Last Post: 08 January 2012 - 10:04 PM

### #1 guyfromri

Reputation: 46
• Posts: 836
• Joined: 16-September 09

# Problems with leap year in date picker

Posted 07 January 2012 - 07:04 PM

hi guys!!

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 -- any thoughts? As always, thanks in advance!!!

Is This A Good Question/Topic? 0

## Replies To: Problems with leap year in date picker

### #2 DimitriV

• They don't think it be like it is, but it do

Reputation: 585
• Posts: 2,738
• Joined: 24-July 11

## Re: Problems with leap year in date picker

Posted 07 January 2012 - 07:13 PM

Not all years divisible by 4 are leap years. The centuries must also be divisible by 400. Ie
1600 - leap year
1700 - not a leap year
2000 - leap year

### #3 guyfromri

Reputation: 46
• Posts: 836
• Joined: 16-September 09

## Re: Problems with leap year in date picker

Posted 07 January 2012 - 07:15 PM

Thanks Jimmy...

I took that in to consideration but I'm using this for DOB's so really my scope is like 1930 ->.

I didn't take that in to consideration thought so if I ever need a really big date picker I'll definitely have that in the back of my head.

Thanks!!

### #4 Dormilich

• 痛覚残留

Reputation: 3688
• Posts: 11,093
• Joined: 08-June 10

## Re: Problems with leap year in date picker

Posted 08 January 2012 - 04:14 AM

the leap year calculation goes like:
- divisible by 4 - leap
- divisible by 100 - no leap
- divisible by 400 - leap

(this approximates the year to 365.2425 days (of 365.242190))

### #5 guyfromri

Reputation: 46
• Posts: 836
• Joined: 16-September 09

## Re: Problems with leap year in date picker

Posted 08 January 2012 - 07:16 PM

Dormilich, on 08 January 2012 - 06:14 AM, said:

the leap year calculation goes like:
- divisible by 4 - leap
- divisible by 100 - no leap
- divisible by 400 - leap

(this approximates the year to 365.2425 days (of 365.242190))

```if(((yr % 4 == 0) && (yr % 100 != 0)) || (yr % 400 == 0))

```

To help someone else out in the future!! thanks guys!!!!

### #6 DimitriV

• They don't think it be like it is, but it do

Reputation: 585
• Posts: 2,738
• Joined: 24-July 11

## Re: Problems with leap year in date picker

Posted 08 January 2012 - 10:04 PM

Dormilich, on 08 January 2012 - 09:14 PM, said:

the leap year calculation goes like:
- divisible by 4 - leap
- divisible by 100 - no leap
- divisible by 400 - leap

(this approximates the year to 365.2425 days (of 365.242190))

Not really... if its divisible by 100 but not 400 then no. But if it is divisible by 400 and 100 then yes.
What you said kinda supported that anyway.

This post has been edited by jimmyBo: 08 January 2012 - 10:09 PM