Make a structure for date containing two function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Zee Malik
    New Member
    • Jan 2014
    • 6

    #1

    Make a structure for date containing two function

    Make a structure for date containing two function one for increment Date() that add to date if day increase month also increase.
    2nd for validation for date like day is not greater than 30 or 31.and validate the user input before user entered value.

    i have done second.
    but no idea for first one.

    hint for increment date.
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    OK, so you have some kind of structure that represents a date, correct? I'd assume this has a field for a day and one for a month and these are probably some kind of numbers (short, integer, ...), right?

    Now, the gregorian calendar (which is the one used in most of the world today) has different numbers of days in different months. So you should have some way of representing the days per month for each month.
    So, check which month it is in your date and find out the maximum day it can be. Then compare your current date to it. There are two possible results:
    1. Your day is smaller than the maximum. Just increment it. It's all fine.
    2. Your day is the maximum. Set it to 1. Now check, whether it is December; if so, the next month is January (12 -> 1), otherwise it's just the next month in the list. Change the month accordingly.

    The exact implementation will of course depend on your way of representing dates but the general idea will work in any case.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      The first is just like counting
      1. Start with a number
      2. Current column is unit column
      3. Increment the current column
      4. If current column is 10
        1. Set current column to 0
        2. Move to next column
        3. goto 3
      5. Finish

      Example
      1. Number = 19
      2. Current column is unit column
      3. Units = 9 + 1 = 10
      4. If current column is 10 (True)
      1. Set units to 0
      2. Move to tens
      3. goto 3

      3. Tens = 1 + 1 = 2
      4. If current column is 10 (False)
      5. Finish result is 20

      Incrementing dates are the same except that each column (day, month and year) have different max values at step 4, when you reset a value it resets to 1 and not 0 and of course the year never resets it can go as high as you want.

      So in pseudo code

      Code:
      Day, Month, Year // Contain valid date
      Day = Day + 1
      If Day > Days in Month
        Day = 1
        Month = Month + 1
        If Month > Months in Year
          Month = 1
          Year = Year + 1
        Endif
      Endif

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Just to highlight a difference between Banfas and my solution: I check and then change the values, he changes the values and then checks whether they are ok (and if not, changes them again). Oh, and he also considered the year, which I didn't. The result should be the same (except for the year thing of course), which version you use depends on which you prefer.

        Comment

        • Zee Malik
          New Member
          • Jan 2014
          • 6

          #5
          you are both right
          Nepomuk talking about 2nd part
          and Banfa talking about 1st part

          Comment

          Working...