Click here to Skip to main content
15,867,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i like to know the code in asp.net using c#.
suppose we have two datetime as:
a) 21/09/2011 12:35
b) 17/10/2011 17:54

then i want to calculate difference between them as:
(_)months (_)days (_)hours (_) minutes

or as

(_)days (_)hours (_) minutes
Posted

it is very easy use TimeSpan class to get difference.

C#
//first convert you dates to DateTime object
DateTime dt1 =Convert.ToDateTime("21/09/2011 12:35");
            DateTime dt2 =Convert.ToDateTime("17/10/2011 17:54");

            TimeSpan objTimeSpan = dt2-dt1;

            int days = objTimeSpan.Days;
            int hours = objTimeSpan.Hours;
            int Min = objTimeSpan.Minutes;
            int sec = objTimeSpan.Seconds;

            MessageBox.Show("days:"+days.ToString() + " hours:"+hours.ToString() + " min:"+Min.ToString() + " secs:"+sec.ToString());


[Edit]string is converted to datetime and display message is added[/Edit] -Uday
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 2-Sep-11 3:26am    
My 5.
--SA
DanHodgson88 2-Sep-11 3:31am    
nice answer 5*
Uday P.Singh 2-Sep-11 3:39am    
my 5 too :)
RaisKazi 2-Sep-11 3:41am    
Perfect Answer 5!.
Using the TImeSpan class is always a good option.
Have a look here[^]. This will answer your question while using the TimeSpan class.
 
Share this answer
 
datetimeB.Subtract(datetimeA).TotalSeconds , .TotalMinutes , .TotalHours
 
Share this answer
 
TimeSpan ts = dt1.Subtract(dt);
 
Share this answer
 
In case you want to calculate the months you can use the DateDiff class of the Time Period Library for .NET[^]:

// ----------------------------------------------------------------------
public void DateDiffSample()
{
  DateTime date1 = new DateTime( 2009, 11, 8, 7, 13, 59 );
  Console.WriteLine( "Date1: {0}", date1 );
  // > Date1: 08.11.2009 07:13:59
  DateTime date2 = new DateTime( 2011, 3, 20, 19, 55, 28 );
  Console.WriteLine( "Date2: {0}", date2 );
  // > Date2: 20.03.2011 19:55:28

  DateDiff dateDiff = new DateDiff( date1, date2 );

  // description
  Console.WriteLine( "DateDiff.GetDescription(1): {0}", dateDiff.GetDescription( 1 ) );
  // > DateDiff.GetDescription(1): 1 Year
  Console.WriteLine( "DateDiff.GetDescription(2): {0}", dateDiff.GetDescription( 2 ) );
  // > DateDiff.GetDescription(2): 1 Year 4 Months
  Console.WriteLine( "DateDiff.GetDescription(3): {0}", dateDiff.GetDescription( 3 ) );
  // > DateDiff.GetDescription(3): 1 Year 4 Months 12 Days
  Console.WriteLine( "DateDiff.GetDescription(4): {0}", dateDiff.GetDescription( 4 ) );
  // > DateDiff.GetDescription(4): 1 Year 4 Months 12 Days 12 Hours
  Console.WriteLine( "DateDiff.GetDescription(5): {0}", dateDiff.GetDescription( 5 ) );
  // > DateDiff.GetDescription(5): 1 Year 4 Months 12 Days 12 Hours 41 Mins
  Console.WriteLine( "DateDiff.GetDescription(6): {0}", dateDiff.GetDescription( 6 ) );
  // > DateDiff.GetDescription(6): 1 Year 4 Months 12 Days 12 Hours 41 Mins 29 Secs
} // DateDiffSample
 
Share this answer
 

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