ASP. Usage example resolution of TimeSpan in NET

  • 2021-01-25 07:25:23
  • OfStack

This article illustrates the use of TimeSpan in ASP.NET, and share it with you for your reference. The details are as follows:

TimeSpan has several attributes: Days, TotalDays, Hours, TotalHours, Minutes, TotalMinutes, Seconds, TotalSeconds, Ticks. Note that there is no TotalTicks.

1. TimeSpan constants and fields

TimeSpan.MaxValue;            // 10675199.02:48:05.4775807
TimeSpan.MinValue;            //-10675199.02:48:05.4775808
TimeSpan.Zero;                //        0.00:00:00.0
TimeSpan.TicksPerDay;         //1 Days of    Tick The number : 864000000000
TimeSpan.TicksPerHour;        //1 Hours of Tick The number : 36000000000
TimeSpan.TicksPerMillisecond; //1 ms Tick The number : 10000
TimeSpan.TicksPerMinute;      //1 Minutes of the Tick The number : 600000000
TimeSpan.TicksPerSecond;      //1 The second Tick The number : 10000000

2. TimeSpan static methods

TimeSpan.Compare();          // contrast 
TimeSpan.Equals();           //=      
TimeSpan.FromDays();         // Build from day to day
TimeSpan.FromHours();        // Set up small hours
TimeSpan.FromMilliseconds(); // Build from milliseconds
TimeSpan.FromMinutes();      // Build from minutes
TimeSpan.FromSeconds();      // From the number of seconds
TimeSpan.FromTicks();        // from Tick For establishing
TimeSpan.Parse();            // Build from string
TimeSpan.ParseExact();       // Creates from a string in the specified format
TimeSpan.TryParse();         // Attempt to build from a string
TimeSpan.TryParseExact();    // Attempts to create a string from the specified format

3. TimeSpan properties

Days;              // Some day  Hours; // Hours part 
Milliseconds;      // Ms part
Minutes;           // Part points
Seconds;           // Second part
Ticks;             //Tick The total number of
TotalDays;         // The total number of days
TotalHours;        // The total number of hours
TotalMilliseconds; // The total number of milliseconds
TotalMinutes;      // The total number of minutes
TotalSeconds;      // The total number of seconds

4. TimeSpan method

Add();       // + CompareTo(); // than 
Duration();  // The absolute value
Equals();    //
Negate();    // The not , + > - , - > +
Subtract();  // -, Add() The manipulation
ToString();  // Format to a string , .Net 4.0 This is a change from previous versions

5. ES46en builds objects

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
        );
}

6. TimeSpan instance

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

Subtract time 1 from time 2 to obtain an instance of TimeSpan.

So time 2 is 9 days, 23 hours, 59 minutes and 59 seconds longer than time 1.

So, Days is 9, Hours is 23, Minutes is 59, Seconds is 59.

Let's look at Ticks again. Tick is one timing cycle, which means one hundred nanosecond, which means one ten millionth of a second. So Ticks here represents how many total time cycles are different, 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 to convert Ticks into days, that is: 8639990000000 / (10000000 * 24 * 3600) = 9.99998842592593.

TotalHours is to convert Ticks into hours, that is: 8639990000000 / (10000000 * 3600) = 239.999722222222.

TotalMinutes is to convert Ticks into minutes, that is: 8639990000000 / (10000000 * 60) = 14399.9833333333.

ES86en is to convert ES87en into seconds, that is: 8639990000000 / (10000000) = 863999.

I hope this article is helpful to your asp.net program design.


Related articles: