Click here to Skip to main content
14,640,670 members
Rate this:
Please Sign up or sign in to vote.
See more:
how do i calculate duration,of years having inserted a calender?
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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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

TimeSpan diff = myCalender.SelectedDate - DateTime.Now; 
DateTime diffCalc = DateTime.MinValue + diff;
int years = diffCalc.Year - 1;
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.
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100