Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I am working on an mysql project in C#. I want to

generate the retirement date automatically at the age of 60 years while

filling the date of Birth. i.e. if the Date of birth is 15/1/1950 then

the date of retirement will be 31/1/2010 and if the date of birth is

01/1/1950 then the date of retirement will be 31/12/2009 one day

earlier and it should calculate leap year as well.
Posted
Updated 25-Apr-13 20:06pm
v2
Comments
Maciej Los 26-Apr-13 2:24am    
Do you want to calculate it in code or in sql query?
Hemant Singh Rautela 26-Apr-13 2:35am    
this is simple mathematics(for become a good programmer you have to learn mathematics-logical as well as caluslative), atleast try first yourself and show which you tried(if not found solution..).
:-)
Alka_26th 29-Apr-13 2:50am    
thx... I've solved it already.....
PIEBALDconsult 26-Apr-13 3:12am    
I see no benefit in calculating it ahead of time. It's not a very useful number. Granted, we're not starving for storage anymore, but why bother? Most employees leave before then. You'll to have recalculate if the policy changes. And there's always the chance that some stupid law will be passed forbidding such things. :rolleyes:
It may be better to populate a separate table yearly or quarterly and insert any employees over n-5 or some such threshold.
charles henington 26-Apr-13 12:51pm    
??!?

C#
DateTime dt = new DateTime(1950, 1, 1);
            TimeSpan day = new TimeSpan(1, 0, 0, 0);
            DateTime retirement = dt.AddYears(60).Subtract(day);
            MessageBox.Show(retirement.ToLongDateString());
 
Share this answer
 
Comments
Philippe Mori 26-Apr-13 18:26pm    
You should call AddDays(-1) instead. When passing from/to daylight saving time, one day can have 23 or 25 hours for example. Using the function AddDay will properly handle those case (you only have to ensure that you do it on local time if this is what you need).
charles henington 26-Apr-13 19:29pm    
The subtract day was only for the minus one day from the end result not ment to subtract for daylight savings
Hi,
string date = "01/1/1950";
DateTime now = Convert.ToDateTime(date);
DateTime next = now.AddYears(60);

this will give correct retirement date but you need to -1 day
 
Share this answer
 
Comments
Alka_26th 29-Apr-13 2:50am    
thx for the help :) I've solved it
[no name] 29-Apr-13 2:57am    
have you solved based on my code?
Since DateTime already provide all required functions, you should be able to figure by yourself what to do. Fo example, it is very easy to call AddYears(60.0).

Then depending on the exact logic you need you can call AddMonths and/or AddDays and if you need to be at the beginning or end of a month... you create another DateTime with current year and month and 1 as the day.

If you need last day of a month, then you first compute first day of next month and then substract one day.

For example to get first day of a month, you do

C#
DateTime date = something;
var firstDayOfMonth = new DateTime(date.Year, date.Month, 1);


You can also copy information about local/UTC time if desired at that point using the appropriate constructor.

To get the last day of that month, you can do:
C#
var lastDayOfMonth = firstDayOfMonth.AddMonth(1.0).AddDays(-1.0);


To be on the safe side, if you want last day of a month first get first day of that month. I'm not sure how .NET will handle AddMonths(1) if we are at a date that does not exist in next month. For example, if we add 1 month to last day of January.
 
Share this answer
 
C#
string[] dDate = dob.Split('/');
                   string dDay = dDate[0];
                   string dMonth = dDate[1];
                   string dYear = dDate[2];
                   if (Convert.ToInt32(dDay) > 01)
                   {
                       if (Convert.ToInt32(dMonth) == 01 || Convert.ToInt32(dMonth) == 03 || Convert.ToInt32(dMonth) == 05 || Convert.ToInt32(dMonth) == 07 || Convert.ToInt32(dMonth) == 08 || Convert.ToInt32(dMonth) == 10 || Convert.ToInt32(dMonth) == 12)
                       {
                           RYear = Convert.ToInt32(dYear) + 60;
                           RMonth = Convert.ToInt32(dMonth);
                           RDay = 31;
                       }
                       else if (Convert.ToInt32(dMonth) == 04 || Convert.ToInt32(dMonth) == 06 || Convert.ToInt32(dMonth) == 09 || Convert.ToInt32(dMonth) == 11)
                       {
                           RYear = Convert.ToInt32(dYear) + 60;
                           RMonth = Convert.ToInt32(dMonth);
                           RDay = 30;
                       }
                       else if (Convert.ToInt32(dMonth) == 02)
                       {
                           if ((Convert.ToInt32(dYear) % 4) == 0)
                           {
                               if ((Convert.ToInt32(dYear) % 100) == 0)
                               {
                                   if ((Convert.ToInt32(dYear) % 400) == 0)
                                   {
                                       RYear = Convert.ToInt32(dYear) + 60;
                                       RMonth = Convert.ToInt32(dMonth);
                                       RDay = 29;
                                   }
                                   if ((Convert.ToInt32(dYear) % 400) != 0)
                                   {
                                       RYear = Convert.ToInt32(dYear) + 60;
                                       RMonth = Convert.ToInt32(dMonth);
                                       RDay = 28;
                                   }
                               }
                               if ((Convert.ToInt32(dYear) % 100) != 0)
                               {
                                   RYear = Convert.ToInt32(dYear) + 60;
                                   RMonth = Convert.ToInt32(dMonth);
                                   RDay = 29;
                               }
                           }
                           if ((Convert.ToInt32(dYear) % 4) != 0)
                           {
                               RYear = Convert.ToInt32(dYear) + 60;
                               RMonth = Convert.ToInt32(dMonth);
                               RDay = 28;
                           }
                       }
//like wise we can do for month 01 but here retirement month will be minus 1 from curretnt month
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900