TimeSpan usage in ASP. NET, asp. nettimespan

Source: Internet
Author: User

TimeSpan usage in ASP. NET, asp. nettimespan

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.

5. TimeSpan build object

Protected void button#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
);
}

Evaluate the function of the latest posting time
Public string Da (www.111cn.net) teStringFromNow (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 ";
}
}

Common members in DateTime and TimeSpan and their descriptions


Attribute description
Date to obtain the Date part of this instance.
Day.
DayOfWeek obtains the day of the week represented by this instance.
The date that DayOfYear obtains this instance is the day of the year.
Hour obtains the Hour of the date represented by this instance.
Millisecond gets the Millisecond portion of the date represented by this instance.
Minute obtains the Minute of the date represented by this instance.
Month gets the Month of the date represented by this instance.
Now creates a DateTime instance, which is the current local date and time on this computer.
Second obtains the Second part of the date represented by this instance.
TimeOfDay obtains the current day of the instance.
Today to obtain the current date.
Year gets the Year of the date that this instance represents.
Table 01: common attributes of the DateTime class and their descriptions

Method description
Add adds the specified TimeSpan value to the value of this instance.
AddDays adds the specified number of days to the value of this instance.
AddHours adds the specified hours to the value of this instance.
AddMilliseconds adds the specified number of milliseconds to the value of this instance.
AddMinutes adds the specified number of minutes to the value of this instance.
AddMonths adds the specified number of months to the value of this instance.
AddSeconds adds the specified number of seconds to the value of this instance.
AddYears adds the specified number of copies per year to the value of this instance.
DaysInMonth returns the number of days of the specified month in the specified year.
IsLeapYear indicates whether the specified year is a leap year.
Parse converts the specified string of date and time to its equivalent DateTime instance.
Subtract minus the specified time or duration from this instance.
ToLongDateString converts the value of this instance to its equivalent long date string representation.
ToLongTimeString converts the value of this instance to its equivalent long-time string representation.
Tow.timestring converts the value of this instance to its equivalent short-time string representation.
ToShortDateString converts the value of this instance to its equivalent short date string representation.

Table 02: common methods and descriptions of DateTime Structures

Table 03 and table 04 are common attributes and common methods of the TimeSpan structure and their descriptions:

Attribute description
Days.
Hours gets the total number of Hours represented by this instance.
Milliseconds obtains the total number of Milliseconds represented by this instance.
Minutes obtains the total number of Minutes represented by this instance.
Seconds obtains the whole number of Seconds represented by this instance.
Ticks gets the value of this instance in scale.
TotalDays gets the value of this instance, expressed in the whole day and decimal part of the day.
TotalHours gets the value of this instance in the hour and hour decimal part.
TotalMilliseconds obtains the value of this instance in milliseconds and in milliseconds.
TotalMinutes gets the value of this instance in the form of the number of minutes and the decimal part of the minute.
TotalSeconds obtains the value of this instance in the integer and second decimal part.
Table 03: common attributes of the TimeSpan structure and their descriptions

Method description
Add adds the specified TimeSpan to this instance.
Duration returns the TimeSpan whose value is the absolute value of the instance.
FromDays returns the TimeSpan of the specified number of days. The specified number of days is accurate to the nearest millisecond.
FromHours returns the TimeSpan of the specified number of hours. The specified number of hours is accurate to the nearest millisecond.
FromMilliseconds returns a TimeSpan of a specified number of milliseconds.
FromMinutes returns a TimeSpan that specifies the number of minutes. the specified number of minutes is accurate to the nearest millisecond.
FromSeconds returns the TimeSpan of the specified number of seconds. the specified number of seconds is accurate to the nearest millisecond.
Subtract the specified TimeSpan from this instance.
Table 04 common methods of TimeSpan structure and descriptions from: http://www.111cn.net/net/net/58093.htm

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.