C # Time Interval and time Addition

Source: Internet
Author: User

I. Time Interval

/// <Summary> time interval </Summary>
/// <Param name = "datetime1"> first date and time </param>
/// <Param name = "datetime2"> Second Date and time </param>
/// <Returns> integer portion of minutes separated by the same day </returns>
Private int datediff (datetime datetime1, datetime datetime2)
{
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 convert. toint32 (TS. totalminutes );
}

2. The time difference from the current time is several seconds

Datetime dt1 = datetime. now;

Datetime dt2 = datetime. now;

Timespan Ts = dt1.subtract (dt2). duration ();

Double doulen = ts. totalseconds;

3. Time Addition

//

time addition
//
//
// time and
private datetime datesum (datetime datetime1, datetime datetime2)
{< br> datetime1 = hour (datetime2.hour);
datetime1 = datetime1.addminutes (datetime2.minute);
datetime1 = datetime1.addseconds (datetime2.second );
return datetime1;
}

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.