C # common Datetime skills,

Source: Internet
Author: User
Tags date1

C # common Datetime skills,


C # common skills


// Today

DateTime. Now. Date. tow.datestring ();

// Subtract one from yesterday, that is, today's date

DateTime. Now. AddDays (-1). tow.datestring ();

// Tomorrow, similarly, add one

DateTime. Now. AddDays (1). tow.datestring ();

// This Week (to know the day of the week, you must first know the day of the week.

The first day of the week is the day of the day a few days ago. Each week starts from Sunday to Saturday. [0-6])

DateTime. Now. AddDays (Convert. ToDouble (0-Convert.ToInt16

(DateTime. Now. DayOfWeek). to1_datestring ();

// DateTime. Now. DayOfWeek get today is the day of the week (0 <= n <= 6)

DateTime. Now. AddDays (Convert. ToDouble (6-Convert.ToInt16

(DateTime. Now. DayOfWeek). to1_datestring ();

// Display the day of the week in Chinese

// Because DayOfWeek returns the day of the week of the number, you need to convert it into Chinese characters to make it easier for people

Reading, some people will use switches to compare them one by one. In fact, we have better strategies.

String [] Day = new string [] {"Sunday", "Monday", Tuesday "," Wednesday ",

"Thursday", "Friday", "Saturday "};

Day [Convert. ToInt16 (DateTime. Now. DayOfWeek)]; // use it to obtain

// Last week. Similarly, a week is 7 days. Last week is the week minus 7 days. The same is true for next week.

DateTime. Now. AddDays (Convert. ToDouble (0-Convert.ToInt16 (

DateTime. Now. DayOfWeek)-7). to1_datestring ();

DateTime. Now. AddDays (Convert. ToDouble (6-Convert.ToInt16 (

DateTime. Now. DayOfWeek)-7). to1_datestring ();

// Next week

DateTime. Now. AddDays (Convert. ToDouble (0-Convert.ToInt16 (

Datetime. Now. DayOfWeek) + 7). to1_datestring ();

DateTime. Now. AddDays (Convert. ToDouble (6-Convert.ToInt16 (

DateTime. Now. DayOfWeek) + 7). to1_datestring ();

// This month, the first day of this month is the first day of the month, and the last day is the first day of the next month minus one day

DateTime. Now. Year. ToString () + DateTime. Now. Month. ToString (

) + "1"; // The first day

DateTime. Parse (DateTime. Now. Year. ToString () + DateTime. Now.

Month. ToString () + "1"). AddMonths (1). AddDays (-1). to1_datestring ();

// Last day

// Easy to format with the ToString character in C #

DateTime. Now. ToString ("yyyy-MM-01 ");

DateTime. Parse (DateTime. Now. ToString ("yyyy-MM-01"). AddMonths (1). AddDays

(-1). tow.datestring ();

// Last month, minus a month

DateTime. Parse (DateTime. Now. ToString ("yyyy-MM-01"). AddMonths (-1). to1_datestring ();

DateTime. Parse (DateTime. Now. ToString ("yyyy-MM-01"). AddDays (-1). to1_datestring ();

// Add a month to the next month

DateTime. Parse (Datetime. Now. ToString ("yyyy-MM-01"). AddMonths (1). to1_datestring ();

DateTime. Parse (DateTime. Now. ToString ("yyyy-MM-01"). AddMonths (2). AddDays (-1). tow.string ();

// 7 days later

DateTime. Now. Date. tow.datestring ();

DateTime. Now. AddDays (7). tow.datestring ();

// Seven days ago

DateTime. Now. AddDays (-7). tow.datestring ();

DateTime. Now. Date. tow.datestring ();

// During the current year, it is easy for us to format it with the ToString character and calculate the first and last days of the year.

DateTime. Parse (DateTime. Now. ToString ("yyyy-01-01"). to1_datestring ();

DateTime. Parse (DateTime. Now. ToString ("yyyy-01-01"). AddYears (1). AddDays (-1). to1_string ();

// Previous year

DateTime. Parse (DateTime. Now. ToString ("yyyy-01-01"). AddYears (-1). to1_datestring ();

DateTime. Parse (DateTime. Now. ToString ("yyyy-01-01"). AddDays (-1). to1_datestring ();

// Next year

DateTime. Parse (DateTime. Now. ToString ("yyyy-01-01"). AddYears (1). to1_datestring ();

DateTime. Parse (DateTime. Now. ToString ("yyyy-01-01"). AddYears (2). AddDays (-1). to1_datestring ();

// This quarter, many people will think this is difficult and need to write a long process to judge. In fact, we don't need it. We all know it's a year, four quarters, one quarter, three months.

// First, we push the date to the first month of the current quarter, and then the first day of the month is the first day of the current quarter.

DateTime. Now. AddMonths (0-(DateTime. Now. Month-1) % 3). ToString ("yyyy-MM-01 ");

// Similarly, the last day of this quarter is the first day of the next quarter minus one

DateTime. parse. (DateTime. now. addMonth (3-(DateTime. now. month-1) % 3 )). toString ("yyyy-MM-01 ")). addDays (-1 ). toShortDateString ();

// Next quarter

DateTime. Now. AddMonths (3-(DateTime. Now. Month-1) % 3). ToString ("yyyy-MM-01 ");

DateTime. parse (DateTime. now. addMonths (6-(DateTime. now. month-1) % 3 )). toString ("yyyy-MM-01 "). addDays (-1 ). toShortDateString ();

// Last quarter

DateTime. Now. AddMonths (-3-(DateTime. Now. Month-1) % 3). ToString ("yyyy-MM-01 ");

DateTime. parse (DateTime. now. addMonths (0-(DateTime. now. month-1) % 3 )). toString ("yyyy-MM-01 "). addDays (-1 ). toShortDateString ();

// Obtain the number of days in the current month

Int m = System. DateTime. DaysInMonth (System. DateTime. Now. Year, System. DateTime. Now. Month );

// Obtain the actual working day of a month (that is, not including Saturday)

// Int days = getDays (System. DateTime. Now); call to obtain

Private int getDays (System. DateTime date1)

{

Int m = System. DateTime. DaysInMonth (date1.Year, date1.Month );

Int mm = 0;

For (int I = 1; I <= m; I ++)

{

System. DateTime date = Convert. ToDateTime (date1.Year + "-" + date1.Month + "-" + I );

Switch (date. DayOfWeek)

{

Case System. DayOfWeek. Monday:

Case System. DayOfWeek. Tuessday:

Case System. DayOfWeek. Wednesday:

Case System. DayOfWeek. Thursday:

Case System. DayOfWeek. Friday:

Mm = mm + 1;

Break;

}

}

Return mm;

}

// Obtain valid working days between any two dates (excluding Saturday)

DateTime date1 = Convert. ToDateTime ("2008-8-8 ");

DateTime date2 = Convert. ToDateTime ("2008-10-1 ");

Int days = getDays (date1, date2 );

// Call the above command to obtain

Private int getDays (System. DateTime date1, System. DateTime date2)

{

String m = Microsoft. VisualBasic. DateAndTime. DateDiff (EnumDateCompare. day, date1, date2). ToString ("f0 ");

Int mm = 0;

For (int I = 0; I <= Convert. ToInt32 (m); I ++)

{

System. DateTime date = Convert. ToDateTime (date1.AddDays (I ));

Switch (date. DayOfWeek)

{

Case System. DayOfWeek. Monday:

Case System. DayOfWeek. Tuesday:

Case System. DayOfWeek. Wednesday:

Case System. DayOfWeek. Thursday:

Case System. DayOfWeek. Friday:

Mm = mm + 1;

Break;

}

}

Return mm;

}

// Format output

Private void Page_Load (object sender, System. EventArgs e)

{

System. Globalization. DateTimeFormatInfo myDTFI = new System. Globalization. CultureInfo ("en-US", false). DateTimeFormat;

// China is zh-cn

DateTime myDT = System. DateTime. Now;

Response. Write (myDT. ToString ("f", myDTFI ));

// Obtain Saturday and Sunday of the week

Public static void ConvertDateToWeek (DateTime date, out DateTime firstdate, out DateTime lastdate)

{

DateTime first System. DateTime. Now;

DateTime last = System. DateTime. Now;

Switch (date. DayOfWeek)

{

Case System. DayOfWeek. Monday:

First = date. AddDays (-1 );

Last = date. AddDays (5 );

Break;

Case System. DayOfWeek. Tuesday:

First = date. AddDays (-2 );

Last = date. AddDays (4 );

Break;

Case System. DayOfWeek. Wednesday:

First = date. AddDays (-3 );

Last = date. AddDays (3 );

Break;

Case System. DayOfWeek. Thursday:

First = date. AddDays (-4 );

Last = date. AddDays (2 );

Break;

Case System. DayOfWeek. Friday:

First = date. AddDays (-5 );

Last = date. AddDays (1 );

Break;

Case System. DayOfWeek. Saturday:

First = dateAddDays (-6 );

Last = date;

Break;

Case System. DayOfWeek. Sunday:

First = date;

Last = date. AddDays (6 );

Break;

}

Firstdate = first;

Lastdate = last;

}

// Call

DateTime firstdate = System. DateTime. Now;

DateTime lastdate = System. Datetime. Now;

ConvertDateToWeek (date, out firstdate, out lastdate );

// Obtain the week number of the year for the current date

DateTime dt = Convert. ToDateTime ("2008-8-8 ");

Int weeks = dt. DayOfYear/7 + 1;



Related Article

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.