TimeSpan usage in ASP. NET

Source: Internet
Author: User

TimeSpan usage in ASP. NET

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.
 

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

Common members in DateTime and TimeSpan and their descriptions

 

Attribute Description
Date Obtain the date part of the instance.
Day Obtain the date represented by this instance as the day of the month.
DayOfWeek Obtain the date of the week for this instance.
DayOfYear The date indicated by this instance is the day of the year.
Hour Obtains the hour of the specified date.
Millisecond Obtains the millisecond portion of the date represented by this instance.
Minute Obtains the minute of the date represented by this instance.
Month Obtains the month of the date represented by this instance.
Now Create 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 time of the current day of the instance.
Today Obtain the current date.
Year Obtains the year of the date that this instance represents.

Table 01: common attributes of the DateTime class and their descriptions

Method Description
Add Add the specified TimeSpan value to the value of this instance.
AddDays Add the specified number of days to the value of this instance.
AddHours Add the specified number of hours to the value of this instance.
AddMilliseconds Add the specified number of milliseconds to the value of this instance.
AddMinutes Add the specified number of minutes to the value of this instance.
AddMonths Add the specified number of months to the value of this instance.
AddSeconds Add the specified number of seconds to the value of this instance.
AddYears Add 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 Returns the indication of whether the specified year is a leap year.
Parse Converts a specified string of date and time to an equivalent DateTime instance.
Subtract Subtract 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.
Tow.datestring 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 Obtains the total number of days represented by this instance.
Hours Obtains the total number of hours represented by this instance.
Milliseconds Obtains the total number of milliseconds represented by this instance.
Minutes Obtains the number of minutes represented by this instance.
Seconds Obtains the whole number of seconds represented by this instance.
Ticks Obtains the value of this instance in scale.
TotalDays Obtains the value of this instance in the integer and decimal part of the day.
TotalHours Obtains the value of this instance in the hour or hour.
TotalMilliseconds Obtains the value of this instance in the form of decimal digits in milliseconds.
TotalMinutes Obtains the value of this instance in the form of an integer in minutes.
TotalSeconds Obtains the value of this instance in the integer and second.

Table 03: common attributes of the TimeSpan structure and their descriptions

Method Description
Add Add the specified TimeSpan to this instance.
Duration Returns the TimeSpan value of the absolute value of the instance.
FromDays Returns the TimeSpan of the specified number of days. The specified number of days is precise 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 in 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 Subtract the specified TimeSpan from this instance.

Table 04: Common TimeSpan structure methods and descriptions

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.