write a program which can calculate the date after 1 year when some date is entered.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abhishekmoon007
    New Member
    • Jan 2010
    • 1

    write a program which can calculate the date after 1 year when some date is entered.

    the program should accept the current date, month and year and should calculate 1 year later date.
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    do the math first. you may reach some where

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      What do you mean by "1 year later date"? The trivial answer is to keep the day and month unchanged and simply increment the year. Do you need to do something more complicated, such as:
      • Report what the date will be 365 days in the future;
      • Report what the day of the week is on both the entered date and the future date.


      These more complicated problem statements are affected by whether or not there is an intervening leap year; the trivial approach is not affected by leap year.

      Comment

      • AbhinavR
        New Member
        • Nov 2009
        • 2

        #4
        Here's the program :)

        Just check with year for leap year.

        If(Year %4 == 0)
        {
        if ( Month <=2)
        {
        Year++;
        DateCalc(Day, Month, Year);
        }
        }
        else if(Year+1%4 == 0)
        {
        if (Month > 2)
        {
        Year++;
        DateCalc(Day, Month, Year)
        }
        }
        else
        {
        Year++;
        }


        DateCalc(day, month, year)
        {

        if( day !=1 )
        {
        day-1, month, year;
        }
        else
        {
        if (month ==5 or 7 or 8 or 10 or 12)
        {
        day = 31; month--; year;
        }
        else if (month 2 or 4 or 6 or 9 or 11)
        {
        day = 30; month--; year;
        }
        else if (month == 3 && year%4==0)
        {
        day = 29; month--; year;
        }
        else if (month ==3 && year%4 != 0)
        {
        day = 28; month--; year;
        }
        else //if(month==1)
        {
        day = 31; month = 12; year--;
        }

        }



        Funny Answer Isn't it,
        Its my first one on this site.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          This code does not correctly check for a leap year nor does the DateClac function work.

          Avoid posting code solutions. Instead, post a description of what needs to be done.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Originally posted by donbock
            ...
            These more complicated problem statements are affected by whether or not there is an intervening leap year; the trivial approach is not affected by leap year.
            Not quite true! If today is Feb 29, then one year from now is Mar 1. Leap year is always a factor.

            There are a surprising number of corner cases in date calculations. That's why it is so important to fully and carefully specify the desired behavior. You can achieve a pretty good specification with explanatory text and a few carefully chosen examples.

            A very useful assumption is to take the calendar in use today and project it forward and backward in time. That is, ignore the transition from Julian to Gregorian calendar that occurred at different times with differing impact in different countries.

            Comment

            Working...