C # TimeSpan calculation time difference (time interval)

Source: Internet
Author: User
Tags difference between two times

The TimeSpan structure indicates a time interval.

 

TimeSpan (, days = TimeSpan (, = TimeSpan (-,,,,). duration (); totalDays = TimeSpan (, totalHours = TimeSpan (, hours = TimeSpan (, minutes = TimeSpan (,,,, = TimeSpan (, TimeSpan ts3 = TimeSpan (, = TimeSpan (,,,, ==++-= span-dateTicks =View Code

 

 

 

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

Return "1 second ago ";

}
}

Use TimeSpan in C # To calculate the difference between two times

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.

For example, the result of 12 hours in a day is 1.5.

Int nDays = ts. Days; // Integer Number of Days. The result is 1 for 12 hours per day or 20 hours per day.

/// <Summary>
///Date comparison
/// </Summary>
/// <Param name = "today"> current date </param>
/// <Param name = "writeDate"> input date </param>
/// <Param name = "n"> compare days </param>
<Returns> true is returned if the number of days is greater than and false is returned if the number of days is less than </returns>
Private bool CompareDate (string today, string writeDate, int n)
{
DateTime Today = Convert. ToDateTime (today );
DateTime WriteDate = Convert. ToDateTime (writeDate );
WriteDate = WriteDate. AddDays (n );
If (Today> = WriteDate)
Return false;
Else
Return true;
}

Ii. Common members in DateTime and TimeSpan and their descriptions:

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.

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.

 

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.