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

#1 guyfromri  Icon User is offline

  • D.I.C Addict

Reputation: 46
  • View blog
  • Posts: 817
  • 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  Icon User is offline

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

Reputation: 584
  • View blog
  • 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
Was This Post Helpful? 2
  • +
  • -

#3 guyfromri  Icon User is offline

  • D.I.C Addict

Reputation: 46
  • View blog
  • Posts: 817
  • 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!!
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,324
  • 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))
Was This Post Helpful? 1
  • +
  • -

#5 guyfromri  Icon User is offline

  • D.I.C Addict

Reputation: 46
  • View blog
  • Posts: 817
  • Joined: 16-September 09

Re: Problems with leap year in date picker

Posted 08 January 2012 - 07:16 PM

View PostDormilich, 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))


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



To help someone else out in the future!! thanks guys!!!!
Was This Post Helpful? 0
  • +
  • -

#6 DimitriV  Icon User is offline

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

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Problems with leap year in date picker

Posted 08 January 2012 - 10:04 PM

View PostDormilich, 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1