Time Difference Calculation Code in C # In asp.net,

Source: Internet
Author: User

Time Difference Calculation Code in C # In asp.net,

The simplest method I use is

The Code is as follows:
DateTime dtone = Convert. ToDateTime ("05:32:22 ");
DateTime dtwo = Convert. ToDateTime ("04:20:15 ");
TimeSpan span = dtone. Subtract (dtwo );
Response. Write (span. Days + "day" + span. Hours + "Hour" + span. Minutes + "Minute" + span. Seconds + "second ");

The preceding figure shows the time difference between two pages. The output format is similar to "10 days, 5 hours, 10 minutes, 28 seconds". But if I want to calculate the time difference between the two pages.

Instance 1

The Code is as follows:
/// <Summary>
/// Test the program execution time
/// </Summary>
/// <Param name = "dateBegin"> Start time </param>
/// <Param name = "dateEnd"> end time </param>
/// <Returns> return (seconds), 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 convert
Return
Ts3.TotalMilliseconds. ToString ();
}

This is the most basic and the result is the number of milliseconds.

If you only need your format, you can simply take the first 10 digits.

The Code is 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. It is recommended that you use the truncation method if you need one.
For example

The Code is as follows:
Ts3.ToString ("g"). Substring () 0:00:07. 1
Ts3.ToString ("c"). Substring () 00:00:07
Ts3.ToString ("G"). Substring () 0:00:00

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

Use TimeSpan in C # To calculate the difference between the two times (www.111cn.net)

The Code is as follows:
You can add any time unit between two dates.
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 () + "Hour" + ts. minutes. toString () + "Minute" + ts. seconds. toString () + "seconds ";
Return dateDiff;
}


TimeSpan ts = Date1-Date2;
Double dDays = ts. TotalDays; // Number of days with decimals. For example, if one day is 12 hours, 1.5 is returned.
Int nDays = ts. Days; // Integer Number of Days. The result is 1 for 12 hours per day or 20 hours per day.

Recently published Functions

The Code is as follows:
Public string DateStringFromNow (DateTime dt)
{
TimeSpan span = DateTime. Now-dt;
If (span. TotalDays> 60)
{
Return dt. tow.datestring ();
}
Else if (span. TotalDays> 30)
{
Return "1 month ago ";
}
Else if (span. TotalDays> 14)
{
Return "2 weeks ago ";
}
Else if (span. TotalDays> 7)
{
Return "1 week 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 second ago ";
}
}

From: http://www.111cn.net/net/160/45591.htm


How to automatically generate Abcd and 123 in aspnet

If you use numbers, you can use for directly. If you use letters, you can store a string in advance and use it as a char array... Or directly convert the number to char.

What is the relationship between C # In aspnet and C ++ in C?

ASP. NET seems to support C #, Visual Basic, and Jscrip in three languages.

C # (C sharp) is Microsoft's solution to this problem. C # is the latest and object-oriented programming language. Using a simple C # language structure, these components can be easily converted to XML network services so that they can be called over the Internet in any operating system in any language.
Most importantly, C # enables C ++ programmers to develop programs efficiently without losing the original powerful functions of C/C ++. Because of this inheritance relationship, C # has a great similarity with C/C ++. Developers familiar with similar languages can quickly switch to C #.
1. The modern design of C # can eliminate many common C ++ programming errors.
2. C # languages allow extended metadata defined by types. These metadata can be applied to any object. Project builders can define attributes unique to a domain and apply them to any language element-class, interface, and so on.
3. built-in build Object Model (COM) and based on Windows? API support;
4. Native Pointer is allowed ).
That's all. I recommend a post for you.
Bbs.gxut.edu.cn/...page00001

If you are not familiar with ASP. NET, an ASP. NET tutorial ASP. NET technology details and application examples (C #), but I have not downloaded an electronic tutorial. You can buy a book. It should be inexpensive and easy to find.

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.