Namespaces: System

Assembly: mscorlib (in mscorlib.dll)

The 1.DateTime value type represents a specific date time from January 1, 01 0:0 0 seconds to the December 31, 9999 23:59 59 seconds. Therefore, you can use a DateTime value type to describe any time that is within the scope of your imagination. A datetime value represents a specific moment.

The 2.TimeSpan value contains many properties and methods for accessing or processing a TimeSpan value

The following list covers a subset of these:

Add: Adds to another timespan value.

Days: Returns the TimeSpan value that is calculated in day.

Duration: Gets the absolute value of the timespan.

Hours: Returns the TimeSpan value computed in hours

Milliseconds: Returns the TimeSpan value computed in milliseconds.

Minutes: Returns the TimeSpan value computed in minutes.

Negate: Returns the inverse number of the current instance.

Seconds: Returns the TimeSpan value computed in seconds.

Subtract: Subtracts another TimeSpan value from it.

Ticks: Returns the number of ticks for the timespan value.

Totaldays: Returns the number of days that the TimeSpan value represents.

TotalHours: Returns the number of hours that the timespan value represents.

TotalMilliseconds: Returns the number of milliseconds represented by a timespan value.

Totalminutes: Returns the number of minutes represented by a timespan value.

TotalSeconds: Returns the number of seconds represented by a timespan value.

TimeSpan (10, 20, 30, 40, 50) 10.20:30:40.0500000

TimeSpan (1111, 2222, 3333, 4444, 5555) 1205.22:47:09.5550000

The TimeSpan (Int32, Int32, Int32) initializes the new timespan to the specified number of hours, minutes, and seconds.

The TimeSpan (Int32, Int32, Int32, Int32) initializes the new timespan to the specified number of days, hours, minutes, and seconds.

The TimeSpan (Int32, Int32, Int32, Int32, Int32) initializes the new timespan to the specified number of days, hours, minutes, seconds, and milliseconds.

In ASP. NET, subtract two times, get a TimeSpan instance, TimeSpan has some properties: Days, Totaldays, Hours, TotalHours, Minutes, Totalminutes, Seconds, TotalSeconds, Ticks, note that there is no totalticks.

These property names begin to understand somewhat difficult, but after reading this article, you must be enlightened.

Examples Show

• Time 1 is 2010-1-2 8:43:35;

• Time 2 is 2010-1-12 8:43:34.

Use time 2 minus time 1 to get a TimeSpan instance.

Then time 2 is more than 1 9 days 23 hours 59 minutes 59 seconds.

Well, days is 9,hours or 23,minutes or 59,seconds is 59.

Look again Ticks,tick is a time period, indicating 100 nanoseconds, that is, one out of 10,000 seconds, then Ticks here represents a total difference of how many periods, namely: 9 * 24 * 3600 * 10000000 + 23 * 3600 * 10000000 + 59 * 60 * 10000000 + 59 * 10000000 = 8639990000000. 3600 is the number of seconds in an hour.

Totaldays is the conversion of the Ticks Rewardcash octopus number, namely: 8639990000000/(10000000 * 24 * 3600) = 9.99998842592593.

TotalHours is the conversion of Ticks into hours, namely: 8639990000000/(10000000 * 3600) = 239.999722222222.

Totalminutes is the conversion of Ticks into minutes, namely: 8639990000000/(10000000 * 60) = 14399.9833333333.

TotalSeconds is the conversion of Ticks into seconds, namely: 8639990000000/(10000000) = 863999.

Negative

The above is a late date minus the earlier date, so the value of each property is a positive number, and if it is an earlier date that is less late, the property value is negative.

TimeSpan (

int days,

int hours,

int minutes,

int seconds

)

function to find out the last published time

public string Datestringfromnow (DateTime dt)

{

TimeSpan span = Datetime.now-dt;

if (span. Totaldays > 60)

{

Return dt. ToShortDateString ();

}

else if (span. Totaldays > 30)

{

return "1 months ago";

}

else if (span. Totaldays > 14)

{

Return "2 weeks ago";

}

else if (span. Totaldays > 7)

{

Return "1 weeks 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 seconds ago";

}

}

Use a timespan to calculate the difference of two time in C #

You can reverse the addition of two dates between any one unit of time.

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 () + "hours" + TS. Minutes.tostring () + "minutes" + TS. Seconds.tostring () + "seconds";

return DateDiff;

}

TimeSpan ts = date1-date2;

Double ddays = ts. totaldays;//with fractional days, such as 1 days and 12 hours, the result is 1.5.

int ndays = ts. days;//integer days, 1 days 12 hours or 1 days 20 hours results are 1

Description

The 1.DateTime value type represents a specific date time from January 1, 01 0:0 0 seconds to the December 31, 9999 23:59 59 seconds. Therefore, you can use a DateTime value type to describe any time that is within the scope of your imagination. A datetime value represents a specific moment.

The 2.TimeSpan value contains many properties and methods for accessing or processing a TimeSpan value

The following list covers a subset of these:

Add: Adds to another timespan value.

Days: Returns the TimeSpan value that is calculated in day.

Duration: Gets the absolute value of the timespan.

Hours: Returns the TimeSpan value computed in hours

Milliseconds: Returns the TimeSpan value computed in milliseconds.

Minutes: Returns the TimeSpan value computed in minutes.

Negate: Returns the inverse number of the current instance.

Seconds: Returns the TimeSpan value computed in seconds.

Subtract: Subtracts another TimeSpan value from it.

Ticks: Returns the number of ticks for the timespan value.

Totaldays: Returns the number of days that the TimeSpan value represents.

TotalHours: Returns the number of hours that the timespan value represents.

TotalMilliseconds: Returns the number of milliseconds represented by a timespan value.

Totalminutes: Returns the number of minutes represented by a timespan value.

TotalSeconds: Returns the number of seconds represented by a timespan value.

<summary>

Date comparison

</summary>

<param name= "Today" > Current date </param>

<param name= "Writedate" > Enter date </param>

<param name= "n" > Compare days </param>

<returns> returns true for more than days, less than return false</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;

}

Example code:

<table width= "100%" border= "1" cellspacing= "0" cellpadding= "0" >

<tr>

<td>timespan (' T ', ' 2002-10-1 ', ' 2002-1-1 ') </td>

<td> <!--noahvalue valuename= "TimeSpan (' T ', ' 2002-10-1 ', ' 2002-1-1 ')"--></td>

</tr>

<tr>

<td>timespan (' d ', ' 2002-10-1 ', ' 2002-1-1 ') </td>

<td> <!--noahvalue valuename= "TimeSpan (' d ', ' 2002-10-1 ', ' 2002-1-1 ')"--></td>

</tr>

<tr>

<td>timespan (' h ', ' 2002-10-1 ', ' 2002-1-1 ') </td>

<td> <!--noahvalue valuename= "TimeSpan (' h ', ' 2002-10-1 ', ' 2002-1-1 ')"--></td>

</tr>

<tr>

<td>timespan (' u ', ' 2002-10-1 ', ' 2002-1-1 ') </td>

<td> <!--noahvalue valuename= "TimeSpan (' u ', ' 2002-10-1 ', ' 2002-1-1 ')"--></td>

</tr>

<tr>

<td>timespan (' m ', ' 2002-10-1 ', ' 2002-1-1 ') </td>

<td> <!--noahvalue valuename= "TimeSpan (' m ', ' 2002-10-1 ', ' 2002-1-1 ')"--></td>

</tr>

<tr>

<td>timespan (' s ', ' 2002-10-1 ', ' 2002-1-1 ') </td>

<td> <!--noahvalue valuename= "TimeSpan (' s ', ' 2002-10-1 ', ' 2002-1-1 ')"--></td>

</tr>

<tr>

<td>timespan (' D ', ' 2002-10-1 ', ' 2002-1-1 ') </td> (editor: Catcher in the Rye)

When defining the date data type, you must be aware of the following three points:

1. The date value must be enclosed by a number sign "#".

2. Date data in the date value is optional, if any, must conform to the format "m/d/yyyy".

3. The time data in the date value is optional, if there must be and date data separated by a space, and between seconds and minutes separated by ":".

A The relationship and difference between DateTime and TimeSpan:

DateTime and TimeSpan are visual Basic. NET two main structures used to process time date type data, the difference being that dattime represents a fixed time, and a timespan represents a time interval, that is, a period of time. In the program example described below, the timespan is used for the difference between the current time and the given time.

Two Common members in DateTime and TimeSpan and their descriptions:

The datetime and TimeSpan structures provide a rich range of methods and properties that allow you to work with almost any time date type data directly. Table 01 and Table 02 are the common properties and common methods of the datetime structure and their descriptions:

Property Description

Date gets the day portion of this instance.

Day Gets the date that this instance represents is the day ordinal of the month.

DayOfWeek Gets the date that this instance represents is the day of the week.

DayOfYear gets the date represented by this instance is the day ordinal of the year.

Hour gets the hour portion of the date represented by this instance.

Millisecond gets the millisecond portion of the date represented by this instance.

Minute gets the minute portion of the date represented by this instance.

Month gets the months portion of the date represented by this instance.

Now creates a DateTime instance, which is the current local date and time on this computer.

Second gets the seconds portion of the date represented by this instance.

TimeOfDay gets the time of day for this instance.

Today gets the current date.

Year gets the years portion of the date represented by this instance.

Table 01:datetime Common Properties and descriptions of classes

Method description

Add adds the value of the specified timespan to the value of this instance.

AddDays adds the specified number of days to the value of this instance.

AddHours adds the specified number of 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 years to the value of this instance.

DaysInMonth returns the number of days in the specified month in the specified year.

Isleapyear returns whether the specified year is an indication of leap years.

Parse converts the specified string representation of a date and time to its equivalent datetime instance.

Subtract subtracts 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.

Toshorttimestring 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.

The common method of table 02:datetime structure and its explanation

Table 03 and Table 04 are the common properties and common methods of the timespan structure and their descriptions:

Property Description

Day gets the number of days represented by this instance.

Hours gets the number of full hours represented by this instance.

Milliseconds gets the number of full milliseconds represented by this instance.

Minutes gets the number of full minutes represented by this instance.

Seconds gets the number of full seconds represented by this instance.

Ticks gets the value of this instance represented by a scale.

Totaldays gets the value of this instance expressed as a decimal part of the day and the day.

Totalhours Gets the value of this instance expressed as a decimal part of the whole hour and hour.

TotalMilliseconds gets the value of this instance, expressed in the integer milliseconds and the number of milliseconds.

Totalminutes Gets the value of this instance expressed as a decimal part of the whole minute and minutes.

TotalSeconds Gets the value of this instance, expressed in the integer seconds and the number of seconds.

Common Properties of Table 03:timespan structure and its description

Method description

Add adds the specified timespan to this instance.

Duration returns a timespan whose value is absolute for this instance.

Fromdays returns a TimeSpan that represents the specified number of days, where the specified number of days is accurate to the nearest millisecond.

Fromhours returns a TimeSpan that represents the specified number of hours, where the specified number of hours is accurate to the nearest millisecond.

Frommilliseconds returns a TimeSpan that represents the specified number of milliseconds.

The Fromminutes returns a timespan that represents the specified number of minutes, where the specified number of minutes is accurate to the nearest millisecond.

FromSeconds returns a TimeSpan that represents the specified number of seconds, where the specified number of seconds is exactly the nearest millisecond.

Subtract subtracts the specified timespan from this instance.

C # Calculate time difference with TimeSpan function