Skip to content

To calculate difference between two DateTime Picker control( i.e User Entry)

naveen kumar

Asked Dec 12, 2006, 8:31 PM EST5 answers
Share
LinkedInX
Dear Sir,
 
I am encountering a problem and  needs your support .The details of the issue is mentioned below :
 
Environment details - Database : SQL Server 2000 ; ERP Version : Microsoft GP 9.0, Visual studio-2005.
 
Problem  Statement : How to calculate difference between two DateTime Picker controls( i.e User Entry) in terms of Hours and Minutes only. 

Problem Details:
 
1. We are having one C#  .Net Modular Screen where  there are several DateTime Picker Controls accepting Date and Time ( both)  in terms of TimeTO and TimeFROM.
 
2. The difference of Date and Time(both) from the  controls is being accepted by  their Textbox respectively.
 
3, But, difference of  DateTime Picker( i.e TimeTO and TimeFROM)  gives " Day: Hour: Min: Sec.  And , we want Difference in terms of Hours and Minutes only and all the difference will be finally added to get" Total Hours and Minutes".
 
4, We have used most of properties but not getting the same.
 
 
5. Please provide the .net C# code for window forms,   for the same or any other solution.
 
 
With Regards,
 
Naveen Kumar



5 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    You could try using System.TimeSpan, something along the lines of

    TimeSpan ts = dateTimePicker1.Value - dateTimePicker2.Value;
    0
  • 0
    Thanks am lot for your response...U had asked to to do as such:
    TimeSpan ts = dateTimePicker1.Value - dateTimePicker2.Value;

     But the difference  , i am getting as  for ts is " Day(s): Hours:Min:Sec" ,  in textbox.
     What i want is to get the difference in terms of " Hours: Mins" only in textbox in the format i have written , whatsoever the user enters the date and time for each picker control..

     How to do the same...

    Then i want to add all the difference of DateTime Pickers ( in terms of Hour: Min only ) in another Textbox . how to do the same . 
    0
  • 0
    All the info you need is within TimeSpan, there's several methods within TimeSpan that will do what you require, you might need to run a few conditions with it but it will do what you need. I've not used TimeSpan before, but at a quick glance it looks like you need TimeSpan.Hours and TimeSpan.Minutes.

    If you can't do simple Math functions and update a textbox I'd suggest you read up on the basics of C# programming.
    0
  • 0
    Thaks a lot.... But i am also doing the same what you have asked for....
    My Code is:

      

    TimeSpan ts = new TimeSpan();

    double s1;

    double s2;

    ts = dtp2.Value - dtp1.Value;

    s1 = Convert.ToDouble(ts.TotalHours);

    s2 = Convert.ToDouble(ts.TotalMinutes);

    txt1.Text = String.Format("{0}:{1}", s1, s2);


    But, i am getting the hours in correct format but minutes not in the correct format i.e some junk no.s..

     How to rectify it....

    0
  • 0
    That because TotalMinutes will give you the total amount of minutes between the two dates, you could use TotalMinutes % 60 to get the correct amount.
    0