Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
Dear Frnds,

I need. how to calculate hours between two time pickers... can anyone help me..









Thanks & Regards,
Vivek .R
Posted
Comments
Tejas Vaishnav 30-Apr-14 4:59am    
Convert your time picker value to time stamp and subtract it, it will return time stamp and you can get hours, minuet, seconds etc from subtraction result.
Emre Ataseven 30-Apr-14 5:33am    
http://bit.ly/1ksjMhn

You can subtract a DateTime value from another to get a TimeSpan value. This way:
C#
DateTime d1 = dtPicker1.Value;
DateTime d2 = dtPicker2.Value;
TimeSpan ts = d1 - d2;
double result = ts.TotalHours;
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 30-Apr-14 4:59am    
Very nice! I was sure that we need to use Google for that :-)...
phil.o 30-Apr-14 5:02am    
:)
Volynsky Alex 30-Apr-14 5:04am    
Yes, nice!
Sanket Saxena 30-Apr-14 5:21am    
Quite simple and working code :)
DamithSL 30-Apr-14 5:54am    
what is the date time picker giving direct DateTime as value?
it depend on the date time picker you are currently using, check below steps for jquery date time picker

1.Download date time picker from http://xdsoft.net/jqplugins/datetimepicker/
2.Add jquery.datetimepicker.js, jquery.js and jquery.datetimepicker.css files to asp.net solution and add links
ASP.NET
<head  runat="server">
    <title></title>
    <link rel="stylesheet" type="text/css" href="./jquery.datetimepicker.css" />
    <script type="text/javascript" src="./jquery.js"></script>
    <script type="text/javascript" src="./jquery.datetimepicker.js"></script>
</head>

3. Add two text box for the two date time pickers
ASP.NET
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

4. Need to add javascript to populate the date time picker
JavaScript
<script type="text/javascript">
    $(document).ready(function () {
        $('#TextBox1').datetimepicker();
        $('#TextBox1').datetimepicker({ value: '2015/04/15 05:03', step: 10 });
        $('#TextBox2').datetimepicker();
        $('#TextBox2').datetimepicker({ value: '2015/04/15 05:03', step: 10 });
    });
</script>

5. you can set default date time from server side(page load) like below
C#
TextBox1.Text = DateTime.Now.ToString("yyyy/MM/dd HH:mm");
TextBox2.Text = DateTime.Now.ToString("yyyy/MM/dd HH:mm");

6. date time value can be converted to DateTime as below (on button click)
C#
DateTime datetime1;
DateTime datetime2;
if (DateTime.TryParseExact(TextBox1.Text, "yyyy/MM/dd HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out datetime1) && DateTime.TryParseExact(TextBox2.Text, "yyyy/MM/dd HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out datetime2))
{
    var hours = datetime2.Subtract(datetime1).TotalHours;
}


Sample project can be downloaded from below link
DateTimePicker Sample VS2008 Web Application.zip[^]
 
Share this answer
 
v4
Comments
Vivek.anand34 30-Apr-14 5:37am    
"CultureInfo" "DateTimeStyles" does not exist in the current context error..
Vivek.anand34 30-Apr-14 5:43am    
Error: datetime2 is use of un assigned variable. in above line
var hours = datetime2.Subtract(datetime1).TotalHours;
DamithSL 30-Apr-14 5:51am    
it should be a warning
DamithSL 30-Apr-14 5:43am    
add using System.Globalization; on top of the c# code
Vivek.anand34 30-Apr-14 5:54am    
Picker is not display.. what i have give in page load that only will come..
check following code block from http://msdn.microsoft.com/en-us/library/aa287590(v=vs.71).aspx[^]

DateTime oldDate = new DateTime(2002,7,15);
DateTime newDate = DateTime.Now;

// Difference in days, hours, and minutes.
TimeSpan ts = newDate - oldDate;
// Difference in days.
int differenceInDays = ts.Days;

Console.WriteLine("Difference in days: {0} ", differenceInDays);
 
Share this answer
 
DateTime startTime = convert.todatetime(dtp.text);

DateTime endTime = convert.todatetime(dtp2.text);

TimeSpan span = endTime.Subtract ( startTime );

Console.WriteLine( "Time Difference (hours): " + span.Hours );
 
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