Tag: OS Io Re C time new
In C #, how to calculate the difference between the two periods. Calculate the difference between two times, which can be calculated to the hour, minute, and second!
<1>
Label1.text = "15:36:05 ";
Label2.text = "20:16:35 ";
Datetime d1 = new datetime );
Datetime D2 = new datetime );
Timespan D3 = d2.subtract (D1 );
Label3.text = "difference :"
+ D3.days. tostring () + "day"
+ D3.hours. tostring () + "hour"
+ D3.minutes. tostring () + "Minute"
+ D3.seconds. tostring () + "seconds ";
<2>
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Datetime dt1 = convert. todatetime ("00:00:00 ");
Datetime dt2 = convert. todatetime ("00:00:00 ");
This. label1.text = This. datediff (dt1, dt2 );
}
}
Private string datediff (datetime datetime1, datetime datetime2)
{
String datediff = NULL;
Try
{
Timespan ts1 = new timespan (datetime1.ticks );
Timespan ts2 = new timespan (datetime2.ticks );
Timespan Ts = ts1.subtract (ts2). duration ();
Datediff = ts. days. tostring () + "day" + ts. hours. tostring () + "Hour" + ts. minutes. tostring () + "Minute" + ts. seconds. tostring () + "seconds ";
}
Catch
{}
Return datediff;
}
<3>
String ST1 = "14: 16 ";
String st2 = "14:14 ";
Datetime dt1 = convert. todatetime (ST1 );
Datetime dt2 = convert. todatetime (st2 );
Int result = datetime. Compare (dt1, dt2 );
If the time of ST1 is greater than st2, result = 1
If they are equal to 0,
If the value is smaller than-1