Calculate time Difference code in C # in asp.net

Source: Internet
Author: User
Tags datetime

The easiest way for me to use it

The code is as follows Copy Code

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 + "H" + span. Minutes + "Minutes" + span. seconds+ "seconds");

The above is given two times, the calculation time difference, the output is similar to "10 days 5 hours 10 minutes 28 seconds" format, but if I want to work out two pages to perform the slack.

Instance 1

The code is as follows Copy Code
<summary>
Program Execution Time Test
</summary>
<param name= "Datebegin" > Start time </param>
<param name= "DateEnd" > End time </param>
<returns> return (seconds) unit, for example: 0.00239 sec </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 and gets the number of milliseconds

If you're just a simple need for that format, you can just take the first 10 digits.

The code is as follows Copy Code

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 you can use the Intercept test faster if you need one.
Like what

The code is as follows Copy Code

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

--------------------------------------------------------------------------------

Using TimeSpan in C # to calculate the difference of two times

  code is as follows copy code

can be added to any one time between two dates Between units.
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 () + "days" + TS. Hours.tostring () + "hour" + TS. Minutes.tostring () + "minutes" + TS. Seconds.tostring () + "seconds";
return DateDiff;
}


TimeSpan ts = date1-date2;
Double ddays = ts. totaldays;//number of days with decimals, such as 1 days 12 hours result is 1.5
int ndays = ts. days;//integer days, 1 days 12 hours or 1 days 20 hours results are 1

Functions of the last Published time

The code is as follows Copy Code
public string Datestringfromnow (DateTime dt)
{
TimeSpan span = Datetime.now-dt;
if (span. Totaldays > 60)
{
Return dt. ToShortDateString ();
}
else if (span. Totaldays > 30)
{
return "1 months ago";
}
else if (span. Totaldays > 14)
{
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";
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.