The simplest way I can use it is to
Code to copy code as follows
DateTime Dtone = Convert.todatetime ("2007-1-1 05:32:22");
DateTime dtwo = Convert.todatetime ("2007-1-1 04:20:15");
TimeSpan span = Dtone. Subtract (Dtwo);
Response.Write (span. Days + "Day" + span. Hours + "hour" + span. Minutes + "Minutes" + span. seconds+ "seconds");
The above is given two times, the time difference is calculated, the output is similar to "10 days and 5 hours 10 minutes 28 seconds" format, but if I want to figure out two pages to perform the time difference.
Example 1
Code to copy code as follows
<summary>
Program Execution Time Test
</summary>
<param name= "Datebegin" > Start time </param>
<param name= "DateEnd" > End time </param>
<returns> return (seconds) units, for example: 0.00239 seconds </returns>
public static string Execdatediff (DateTime datebegin, datetime dateend)
{
TimeSpan ts1 = new TimeSpan (datebegin.ticks);
TimeSpan ts2 = new TimeSpan (dateend.ticks);
TimeSpan Ts3 = ts1. Subtract (TS2). Duration ();
The format you want to turn
Return
Ts3. Totalmilliseconds.tostring ();
}
This is the most basic, get the number of milliseconds
If you're the only one who needs you, you can just take the top 10, all right?
Code to copy code as follows
1.ts3. ToString ("G") 0:00:07.171
2.ts3. ToString ("C") 00:00:07.1710000
3.ts3. ToString ("G") 0:00:00:07.1710000
There are three formats to choose from, and I suggest that if you need one, you can use the Intercept test faster
Like what
Code to copy code as follows
Ts3. ToString ("G"). Substring (0,8) 0:00:07.1
Ts3. ToString ("C"). Substring (0,8) 00:00:07
Ts3. ToString ("G"). Substring (0,8) 0:00:00
--------------------------------------------------------------------------------
Use a timespan to calculate the difference of two time (www.111cn.net) in C #
Code to copy code as follows
You can reverse the addition of two dates between any one unit of time.
private String DateDiff (DateTime DateTime1, datetime DateTime2)
{string dateDiff = null;
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 () + "hours" + TS. Minutes.tostring () + "minutes" + TS. Seconds.tostring () + "seconds";
return DateDiff;
}
TimeSpan ts = date1-date2;
Double ddays = ts. totaldays;//with fractional days, such as 1 days and 12 hours, the result is 1.5.
int ndays = ts. days;//integer days, 1 days 12 hours or 1 days 20 hours results are 1
Function of the most recent publication time
The
Code follows copy Code
public string Datestringfromnow (DateTime dt)
{
TimeSpan span = Datetime.now-dt; The
if (span. Totaldays >)
{
Return dt. ToShortDateString ();
}
Else if (span. Totaldays >
{
Return "1 months ago";
}
Else if (span. Totaldays >)
{
return "2 weeks ago";
}
Else if (span. Totaldays > 7)
{
return "1 weeks ago";
}
Else if (span. Totaldays > 1)
{
return string. Format ("{0} days ago", (int) Math.floor (span. Totaldays));
}
Else if (span. TotalHours > 1)
{
return string. Format ("{0} hours ago", (int) Math.floor (span). TotalHours));
}
Else if (span. Totalminutes > 1)
{
return string. Format ("{0} minutes ago", (int) Math.floor (span). totalminutes));
}
Else if (span. TotalSeconds >= 1)
{
return string. Format ("{0} seconds ago", (int) Math.floor (span). TotalSeconds));
}
Else
{
return "1 seconds ago";
}
}
from:http://www.111cn.net/net/160/45591.htm
Calculate the time difference code
in C # in ASP.