SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question JS Leap year issue

    I have the following, but not sure why it isn't picking up this year as a leap year. Any idea?

    Code:
    CalendarMonth.prototype.correctForLeapYear = function()
    {
    	if(this.year%4 != 0)
    	{
    		daysInMonth[1] = 28;
    		this.isLeapYear = 0;
    	 }
    	else if(this.year%400 == 0)
    	{
    		daysInMonth[1] = 29;
    		this.isLeapYear = 1;
    	}
    	else if(this.year%100 == 0)
    	{
    		daysInMonth[1] = 28;
    		this.isLeapYear = 0;
    	 }
    	else
    	{
    		daysInMonth[1] = 29;
    		this.isLeapYear = 1;
    	}
    }
    Thanks
    Martin

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    When it's not a leap year and you set a date to the 29th of February, it will roll over to the first of March.

    Code javascript:
    this.isLeapYear = (new Date(this.year, 1, 29).getDate() === 29);

    That's the best way for you to determine if the year is a leap year.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •