Click here to Skip to main content
15,885,086 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I am trying to write c# code to calculate starting date from total number of months spent at each place. I have number of months and I want to calculate starting date based on today's date and number of months spent.

Any suggestions please, I will appreciate it if anyone can send me a sample code. Thanks in advance.

Regards
JS
Posted

If you subtract the number of months or days from the end date, you will get the start date. There's methods on the DateTime class to AddDays, I'm not sure about months, but if you mean literal months ( irrespective of how many days they had), you can just use the existing day and change the month, right ?
 
Share this answer
 
Comments
jattscorpion 13-Jul-10 18:33pm    
thanks for your reply..
yes there is a method DateTime.Now.AddMonths, but in this case, I want to Subtract months from current date.
Day and date is not a problem, I just want to calculate months and years (an estimated start date).

regards
idenizeni 13-Jul-10 20:00pm    
You can still subtract months using the DateTime.Now.AddMonths method. Just use a negative value, i.e. DateTime.Now.AddMonths(-3) would subtract three months. I believe this works the same for the other Add methods too.
jattscorpion 14-Jul-10 2:38am    
yeah, it works..... :)
thanks alot
try this
string ldDateFrom = ldToDay.ToShortDateString();
string ldDateTo = ldToDay.AddDays(31).ToShortDateString();

hope this help

ahmad zrein
 
Share this answer
 
Comments
[no name] 8-Aug-14 17:02pm    
Wow... really? Do you really think that after 4 years the OP is still waiting for an answer to a question that has already been answered?

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