Asp. Application of TimeSpan in net-practical skills

Source: Internet
Author: User

This article illustrates the usage of timespan in asp.net and share it for everyone's reference. Specifically as follows:

In ASP.net, two times subtract, get a TimeSpan instance, TimeSpan have some attributes: Days, Totaldays, Hours, Totalhours, Minutes, Totalminutes, Seconds, TotalSeconds, Ticks, note that there is no totalticks.

First, TimeSpan constants, fields

Copy Code code as follows:
TimeSpan.MaxValue; 10675199.02:48:05.4775807
Timespan.minvalue; -10675199.02:48:05.4775808
TimeSpan.Zero; 0.00:00:00.0
Timespan.ticksperday; Number of Tick a day: 864000000000
Timespan.ticksperhour; Number of Tick in one hour: 36000000000
Timespan.tickspermillisecond; Number of Tick in a millisecond: 10000
Timespan.ticksperminute; Number of Tick in one minute: 600000000
Timespan.tickspersecond; Number of Tick in a second: 10000000

Second, TimeSpan static method

Copy Code code as follows:
Timespan.compare (); Contrast
Timespan.equals (); //=
Timespan.fromdays (); Build from Days
Timespan.fromhours (); Set up from a small number of hours
Timespan.frommilliseconds (); Based on the number of milliseconds
Timespan.fromminutes (); Set up from minute count
Timespan.fromseconds (); Set from number of seconds
Timespan.fromticks (); Establish from Tick number
Timespan.parse (); Build from String
Timespan.parseexact (); Creates a string from a specified format
Timespan.tryparse (); Attempt to build from string
Timespan.tryparseexact (); Attempt to create a string from a specified format

Third, TimeSpan properties

Copy Code code as follows:
Days; Day part Hours; Hours section
milliseconds; Millisecond section
Minutes; Sub-section
Seconds; Seconds part
Ticks; Total Tick
Totaldays; Total Days
totalhours; Total number of hours
TotalMilliseconds; Total number of milliseconds
Totalminutes; Total number of minutes
TotalSeconds; Total number of seconds

Four, TimeSpan method

Copy Code code as follows:
ADD (); + CompareTo (); Compared to
Duration (); Absolute
Equals (); //
Negate (); Take counter, + >-、-> +
Subtract (); -, the counter manipulation of ADD ()
ToString (); Formatted to a string,. Net 4.0 changes compared to previous versions

V. TimeSpan building Objects

Copy Code code as follows:
protected void Button1_Click (object sender, EventArgs e)
{
TimeSpan T1 = new TimeSpan (864000000000); 1.00:00:00
TimeSpan t2 = new TimeSpan (23, 59, 59); 23:59:59
TimeSpan t3 = new TimeSpan (30, 23, 59, 59); 30.23:59:59
TimeSpan T4 = new TimeSpan (30, 23, 59, 59, 999); 30.23:59:59.9990000

Double F = 365.25;
TimeSpan T5 = timespan.fromdays (f); 365.06:00:00
TimeSpan T6 = timespan.fromhours (f * 24); 365.06:00:00
TimeSpan t7 = Timespan.fromminutes (f * 24 * 60); 365.06:00:00
TimeSpan T8 = Timespan.fromseconds (f * 24 * 60 * 60); 365.06:00:00
TimeSpan T9 = Timespan.frommilliseconds (f * 24 * 60 * 60 * 1000); 365.06:00:00
TimeSpan t0 = timespan.fromticks ((long) (f * 24 * 60 * 60 * 1000 * 10000)); 365.06:00:00

TextBox1.Text = string. Format (' {0}\\n{1}\\n{2}\\n{3}\\n{4}\\n{5}\\n{6}\\n{7}\\n{8}\\n{9} ',
T1, T2, T3, T4, T5, T6, T7, T8, T9, t0
);
}

Vi. Examples of TimeSpan

Time 1 is 2010-1-2 8:43:35;
Time 2 is 2010-1-12 8:43:34.

With time 2 minus time 1, get a TimeSpan instance.

Then 2 times 1 more 9 days 23 hours 59 minutes 59 seconds.

Then, days is 9,hours is 23,minutes is 59,seconds is 59.

To see Ticks,tick is a time period, indicating 100 nanoseconds, that is, one out of 10,000 seconds, then Ticks here to indicate the total difference between the number of periods, namely: 9 * 24 * 3600 * 10000000 + 23 * 3600 * 10000000 + 59 * 60 * 10000000 + 59 * 10000000 = 8639990000000. 3600 is the number of seconds in an hour.

Totaldays is the Ticks conversion own number, namely: 8639990000000/(10000000 * 24 * 3600) = 9.99998842592593.

Totalhours is the conversion of Ticks into hours, that is: 8639990000000/(10000000 * 3600) = 239.999722222222.

Totalminutes is the conversion of Ticks into minutes, that is: 8639990000000/(10000000 * 60) = 14399.9833333333.

TotalSeconds is the conversion of Ticks into seconds, that is: 8639990000000/(10000000) = 863999.

I hope this article will help you with the ASP.net program design.

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.