How to Use timespan in C #

Source: Internet
Author: User
1 eg1.datetime dt1 = datetime. now;
2 datetime dt2 = datetime. Now. adddays (-7 );
3 timespan Ts = dt1-dt2;
4
5 Int days = ts. Days; // The difference between dt1 and dt2
6
7 eg2.
8 // edit whether the month is the same as the month of system Logon
9 If (convert. todatetime (datetime. Now. tow.datestring (). month = convert. todatetime (this. getquerystring ("sdate"). month)
10 {
11 timespan Ts = convert. todatetime (datetime. Now. tow.datestring ()-convert. todatetime (this. getquerystring ("sdate "));
12 idays = ts. days;
13 // You can edit the last three days after logging on to the system.
14 if (idays <4)
15 {
16 submiter submit = new submiter (this, "TT ");
17 this. iseretail. selectedvalue = This. getquerystring ("RID ");
18 this.txt time. Text = This. getquerystring ("sdate"). tostring ();
19 this. inputselect1.wherecondition = "gxlshd = '" + this. getquerystring ("RID") + "'";
20 if (this. getquerystring ("workid ")! = "")
21 This. inputselect1.selectedvalue = This. getquerystring ("workid ");
22 submit. initinsertmode ();
23 setdatagrid ();
24}
25 eg3.c # calculate the difference between the two time points
26
27 /// <summary>
28 // calculate the interval between two dates
29 /// </Summary>
30 /// <Param name = "datetime1"> first date and time </param>
31 /// <Param name = "datetime2"> Second Date and time </param>
32 /// <returns> </returns>
33 private string datediff (datetime datetime1, datetime datetime2)
34 {
35 string datediff = NULL;
36
37 timespan ts1 = new timespan (datetime1.ticks );
38 timespan ts2 = new timespan (datetime2.ticks );
39 timespan Ts = ts1.subtract (ts2). duration ();
40 datediff = ts. Days. tostring () + "day"
41 + ts. Hours. tostring () + "hour"
42 + ts. Minutes. tostring () + "Minute"
43 + ts. Seconds. tostring () + "seconds ";
44
45 return datediff;
46} 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.

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.