C # Summary of datetime-type operations

Source: Internet
Author: User

1. Take the last day of a month
Method 1: calculate the number of days and years of the month.+Month+The number of days that can be added. For example, the last day of the month is used.

Private void getlastdateformonth (datetime dtstart, out datetime dtend)
{
Int dtyear, dtmonth;

Dtstart = datetime. now;
Dtyear = dtstart. Year;
Dtmonth = dtstart. month;

Int monthcount = datetime. daysinmonth (dtyear, dtmonth );
Dtend = convert. todatetime (dtyear. tostring () + "-" + dtmonth. tostring () + "-" + monthcount );

}

Method 2: retrieve the first day of next month minus one day is the last day.

Private void getlastdateformonth (datetime dtstart, out datetime dtend)
{
Int dtyear, dtmonth;

Dtstart = datetime. Now. addmonths (1 );
Dtyear = dtstart. Year;
Dtmonth = dtstart. month;

Dtend = convert. todatetime (dtyear. tostring () + "-" + dtmonth. tostring () + "-" + "1"). adddays (-1 );

}

 Ii. Calculation of time difference
Method 1: UseTimespanAnd also introducesTimespanUsage
 
Related attributes and functions

Add : With another Timespan Value addition.
Days: Returns Timespan Value.
Duration: Obtain Timespan .
Hours: Returns Timespan Value
Milliseconds: Returns the value in milliseconds. Timespan Value.
Minutes: Returns the value calculated in minutes. Timespan Value.
Negate: Returns the reverse Number of the current instance.
Seconds: Returns the value calculated in seconds. Timespan Value.
Subtract: Subtract another Timespan Value.
Ticks: Return Timespan Value Tick Number.
Totaldays: Return Timespan The number of days.
Totalhours: Return Timespan The number of hours.
Totalmilliseconds: Return Timespan The number of milliseconds.
Totalminutes: Return Timespan The number of minutes.
Totalseconds: Return Timespan The number of seconds.  
 

Simple Example:
Datetime d1 = new datetime );
Datetime D2 = new datetime );

Timespan D3 = d2.subtract (D1 );

Lbtime. Text ="Difference:"
+ D3.days. tostring () +"Days"
+ D3.hours. tostring () +"Hours"
+ D3.minutes. tostring () +"Minutes"
+ D3.seconds. tostring () +"Seconds";

Method 2: UseSQLInDatediffFunction
Usage:Datediff(Datepart, Startdate, Enddate)
It can help you get the time difference in various forms you want, such as how many days, how many hours, and how many minutes are separated. The specific format is as follows:

Date

Abbreviations

Year

YY, yyyy

Quarter

QQ, Q

Month

Mm, m

Dayofyear

Dy, y

Day

DD, d

Week

Wk, WW

Hour

HH

Minute

Mi, n

Second

SS, S

Millisecond

MS

For example:Datediff (MI, dtoptime, dtend)The total number of minutes of time difference between them can be retrieved, which has been converted for you. It is particularly useful for specifying the unit, namely, hour, minute, and second.

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.