TimeSpan usage in ASP. NET, asp. nettimespan

Source: Internet
Author: User

TimeSpan usage in ASP. NET, asp. nettimespan

ASP. in. NET, two timespans are subtracted to get a TimeSpan instance. TimeSpan has some attributes: Days, TotalDays, Hours, TotalHours, Minutes, TotalMinutes, Seconds, TotalSeconds, and Ticks.

 

I. 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; // The number of ticks per day: 864000000000
TimeSpan. TicksPerHour; // The number of Tick in one hour: 36000000000
TimeSpan. TicksPerMillisecond; // The number of Tick data in one millisecond: 10000
TimeSpan. TicksPerMinute; // Number of Tick requests per minute: 600000000
TimeSpan. TicksPerSecond; // The number of Tick in one second: 10000000

 

Ii. Static TimeSpan Method

 

TimeSpan. Compare (); // comparison
TimeSpan. Equals (); // =
TimeSpan. FromDays (); // create from days
TimeSpan. FromHours (); // create
TimeSpan. FromMilliseconds (); // It is created in milliseconds.
TimeSpan. FromMinutes (); // create from the number of minutes
TimeSpan. FromSeconds (); // created in seconds
TimeSpan. FromTicks (); // create from the number of Tick
TimeSpan. Parse (); // create from string
TimeSpan. ParseExact (); // create a string in the specified format
TimeSpan. TryParse (); // try to create
TimeSpan. TryParseExact (); // try to create a string from the specified format
 

 

3. TimeSpan attributes

 

Days; // Day Part Hours; // Hour Part
Milliseconds; // millisecond part
Minutes; // parts
Seconds; // Seconds
Ticks; // total number of Tick
TotalDays; // Total Number of days
TotalHours; // total hours
TotalMilliseconds; // The total number of milliseconds
TotalMinutes; // The total number of minutes
TotalSeconds; // The total number of seconds.


 

IV,TimeSpan Method

 

Add (); // + CompareTo (); // comparison
Duration (); // absolute value
Equals ();//
Negate (); // returns the inverse, +>-,-> +
Subtract (); //-, Add () Anti-manipulation
ToString (); // format it to a string. the. Net 4.0 version has changed from the previous version.
 

 

V,TimeSpan build object
 

C # code Replication
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. TimeSpan instances

C # code Replication
Time 1 is 8:43:35; time 2 is 8:43:34. Use Time 2 minus time 1 to get a TimeSpan instance. Then the time 2 is 9 days, 23 hours, 59 minutes, and 59 seconds later than the time 1. In this case, Days is 9, Hours is 23, Minutes is 59, and Seconds is 59. Let's take a look.TicksTick is a time cycle, indicating one hundred nanoseconds, that is, one thousandth of a second. Here, Ticks indicates the total difference of time periods, that is: 9*24*3600*10000000 + 23*3600*10000000 + 59*60*10000000 + 59*10000000 = 8639990000000. 3600 is the number of seconds in an hour.TotalDaysTicks is converted to the daily number, that is, 8639990000000/(10000000*24*3600) = 9.99998842592593.TotalHoursIt is to convert Ticks into hours, that is, 8639990000000/(10000000*3600) = 239.999722222222.TotalMinutesTicks is converted to the number of minutes, that is, 8639990000000/(10000000*60) = 14399.9833333333.TotalSecondsIt is to convert Ticks into seconds, that is, 8639990000000/(10000000) = 863999.

How to obtain system time in aspnet

DateTime. Now. ToString ("yyyy-MM-dd ")

// Use TimeSpan in C # To calculate the difference between the two time points
// 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.

/// <Summary>
/// Calculate the interval between two dates
/// </Summary>
/// <Param name = "DateTime1"> first date and time </param>
/// <Param name = "DateTime2"> Second Date and time </param>
/// <Returns> </returns>
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"
... The remaining full text>

C # What is the Timespan structure?

The TimeSpan object indicates the time interval or duration, which is measured by plus or minus days, hours, minutes, seconds, and decimals of seconds. The maximum unit of time used to measure the duration is day. A larger unit of time (such as Month and year) has different days. Therefore, to maintain consistency, the time interval is measured in days.

The value of a TimeSpan object is equal to the number of scales for the time interval. A scale is equal to 100 nanoseconds. The value of a TimeSpan object ranges from MinValue to MaxValue.

The TimeSpan value can be [-] d. hh: mm: ss. ff. The minus sign is optional. It indicates the negative time interval. The d Component indicates the day, hh indicates the hour (in 24-hour format), mm indicates the minute, and ss indicates the second, ff is the fractional part of the second. That is, the time interval includes the total plus or minus days, the number of days, and the remaining duration of less than one day, or only the duration of less than one day. For example, the text of a TimeSpan object initialized to 1.0e + 13 indicates "11.13: 46: 40", that is, 11 days, 13 hours, 46 minutes, and 40 seconds.

The TimeSpan type implements the System...:. IComparable and System...:. IComparable <(Of <(T>)> interfaces.
Reference: MSDN

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.