Comparison time timespan

Source: Internet
Author: User
Tags difference between two times

The user is not allowed to vote repeatedly within several hours.

Current Time-voting time> allowed voting time

DateTime time1 = System. DateTime. Now;

DateTime time2 = DateTime. Parse ("11:19:17 ");
TimeSpan time3 = time2-time1;

More details ========

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

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.
Int ndays = ts. Days; // Integer Number of days. The result is 1 for 12 hours per day or 20 hours per day.

Note:
1. The datetime Value Type represents a specific date time between 00:00:00 on January 1, January 1, 0001 AD and on January 1, December 31, 9999 AD. Therefore, you can use the datetime value type to describe any time within the expected range. A datetime value represents a specific time.
2. the timespan value contains many attributes and methods for accessing or processing a timespan value.
The following list covers some of them:
Add: adds the value to another timespan value.
Days: returns the timespan value calculated by the number of days.
Duration: gets the absolute value of TimeSpan.
Hours: returns the hourly TimeSpan value.
Milliseconds: returns the TimeSpan value in Milliseconds.
Minutes: returns the TimeSpan value calculated in Minutes.
Negate: returns the opposite number of the current instance.
Seconds: returns the TimeSpan value calculated in Seconds.
Subtract: Subtract another TimeSpan value from it.
Ticks: the number of tick values returned for TimeSpan.
TotalDays: returns the number of days indicated by the TimeSpan value.
TotalHours: returns the number of hours represented by the TimeSpan value.
TotalMilliseconds: the number of milliseconds in which the TimeSpan value is returned.
TotalMinutes: returns the number of minutes represented by the TimeSpan value.
TotalSeconds: returns the number of seconds represented by the TimeSpan value.

/// <Summary>
/// Date comparison
/// </Summary>
/// <Param name = "today"> current date </param>
/// <Param name = "writeDate"> input date </param>
/// <Param name = "n"> compare days </param>
/// <Returns> returns true if it is greater than the number of days, and false if it is less than the return value </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;
}

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.