Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how do i calculate duration,of years having inserted a calender?
Posted
Comments
ckulasekaran 28-Feb-12 4:28am    
Using datetimepicker and use nodays = System.DateTime.DaysInMonth(iyear, imonth)
Tejas Vaishnav 28-Feb-12 4:41am    
not clear please provide more description

1 solution

Timespan does not include years (because it doesn't know when the time span starts):

C#
TimeSpan diff = myCalender.SelectedDate - DateTime.Now; 
DateTime diffCalc = DateTime.MinValue + diff;
int years = diffCalc.Year - 1;
 
Share this answer
 
Comments
Manfred Rudolf Bihy 28-Feb-12 4:43am    
Nice one! 5+

Had to look up DateTime.MinValue and yes it starts in the year one: 00:00:00.0000000, January 1, 0001.
:doh:
OriginalGriff 28-Feb-12 5:07am    
Yes, it's a PITA, but understandable - since Timespan doesn't know then the time starts, it can't give you months or years and you have to play silly buggers! :laugh:

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