How to calculate leap year

mistu2020mistu2020 Member Posts: 15
edited 2014-02-17 in NAV Three Tier
I want to calculate no of days.

If leap year then the no of days will be 366 other wise 365.

How to do this????

Comments

  • mohana_cse06mohana_cse06 Member Posts: 5,504
    If year MOD 4 = 0 then leap year

    ex: 2014 MOD 4 = 2 means not a leap year
    2016 MOD 4 = 0 means Leap year.
  • lvanvugtlvanvugt Member Posts: 774
    Luc van Vugt, fluxxus.nl
    Never stop learning
    Van Vugt's dynamiXs
    Dutch Dynamics Community
  • geordiegeordie Member Posts: 655
    Another alternative:
    NoOfDays := DMY2DATE(31,12,Year) - DMY2DATE(31,12,Year - 1);
    

    (Simultaneous posting :oops: )
  • Marije_BrummelMarije_Brummel Member, Moderators Design Patterns Posts: 4,262
    You can also use the virtual date table. Count the days in a year. It's an in memory process, should be fast.
  • ta5ta5 Member Posts: 1,164
    Just curiuos:
    How about use a .NET Class for this?
    Thomas
  • Marije_BrummelMarije_Brummel Member, Moderators Design Patterns Posts: 4,262
  • lvanvugtlvanvugt Member Posts: 774
    Good thinking Thomas, we should think .net these days.
    :thumbsup:
    Luc van Vugt, fluxxus.nl
    Never stop learning
    Van Vugt's dynamiXs
    Dutch Dynamics Community
  • ta5ta5 Member Posts: 1,164
    Good thinking Thomas, we should think .net these days.

    And IMO at least as important: We should not re-invent the wheel, this was the reason for my first answer.

    Regards
    Thomas
  • MBergerMBerger Member Posts: 413
    Good thinking Thomas, we should think .net these days.

    True, but in this case it seems to me that using .net to determine if a year is a leap year is a bit overkill and more using the technology for the sake of having used the technology......

    It is as when the Americans met the Russians for the first time in space : The US had invested millions in making a pen with a pressurized ink cartridge so it would write in zero gravity. When they asked the russians how they solved that particular problem, the Russians answered : "we just use pencils".

    [edit] To add yet another way to determine the leap year : Check the date table if February 29th exists for that year.
  • krikikriki Member, Moderator Posts: 9,112
    And this is pure Russian-style C/AL 8) :
    TheYear := 2012;
    
    Date.GET(Date."Period Type"::Month,DMY2DATE(1,2,TheYear));
    IF DATE2DMY(NORMALDATE(Date."Period End"),1) = 29 THEN
      MESSAGE('Year %1 is a leap year',TheYear);
    


    BTW: Mark, your link does not work. This is the correct link : http://msdn.microsoft.com/en-us/library/system.datetime.isleapyear(v=vs.110).aspx
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • beranberan Member, Microsoft Employee Posts: 80
    I have won many a beer on a debate when year is a leap year.

    A leap year is a year that can be divided by 4 unless it can be divided by 100 and again if it can be divided by 400.

    So 1700, 1800, 1900 was not a leap year, but 2000 was, and 2100 will not be a leap year.

    And for calculation in practice then do take some .net class that can calculate it as many suggest or your AL code. If date between February 28 <year> to March 1 <year> is equal to 2 then a leap year otherwise not.
    Eric Beran
    Software Design Engineer II
    Dynamics NAV Office 365
    Microsoft

    This posting is provided "AS IS" with no warranties, and confers no rights.
  • einsTeIn.NETeinsTeIn.NET Member Posts: 1,050
    beran wrote:
    I have won many a beer on a debate when year is a leap year.

    A leap year is a year that can be divided by 4 unless it can be divided by 100 and again if it can be divided by 400.

    So 1700, 1800, 1900 was not a leap year, but 2000 was, and 2100 will not be a leap year.
    Me too :lol:

    But I'll need to pay as much beer if my software still runs in 2100. Because year 2000 can be devided by 400 I sometimes tend to just divide by 4. I know this is not accurate but I believe it's sufficient for the scope of my code.

    Here's another solution:
    IF DATE2DMY(CALCDATE('<CM>',DMY2DATE(1,2,MyYear)),1) = 29 THEN
      LeapYear := TRUE;
    
    "Money is likewise the greatest chance and the greatest scourge of mankind."
  • ta5ta5 Member Posts: 1,164
    This discussion shows (to me at least) that normally you are better off using a built in function, in this case the .NET. Of course .NET function could be wrong, but I trust it's not... :)
    Thomas
  • einsTeIn.NETeinsTeIn.NET Member Posts: 1,050
    To be honest, the creation of the Date table is also a built-in function. So, I would trust it also.
    "Money is likewise the greatest chance and the greatest scourge of mankind."
  • PerJuhlPerJuhl Member Posts: 55
    If year MOD 4 = 0 then leap year

    ex: 2014 MOD 4 = 2 means not a leap year
    2016 MOD 4 = 0 means Leap year.

    This will not work, as 1700, 1800, 1900 and 2100 eg. are not leap years.

    BR Per
  • PerJuhlPerJuhl Member Posts: 55
    geordie wrote:
    Another alternative:
    NoOfDays := DMY2DATE(31,12,Year) - DMY2DATE(31,12,Year - 1);
    

    (Simultaneous posting :oops: )

    :thumbsup:
Sign In or Register to comment.