C # obtain the system time and time format conversion,

Source: Internet
Author: User
Tags iso 8601

C # obtain the system time and time format conversion,

-- DateTime numeric type
System. DateTime currentTime = new System. DateTime ();
Returns the current year, month, day, hour, minute, second, currentTime = System. DateTime. Now;
Int Year = currentTime. Year;
Int Month = currentTime. Month;
Take int Day = currentTime. Day of the current Day;
= CurrentTime. Hour;
Int = currentTime. Minute;
Int Second = currentTime. Second;
Take the current Millisecond int Millisecond = currentTime. Millisecond; (the variable can be in Chinese)

Display Chinese date -- year, month, day, string strY = currentTime. ToString ("f"); // do not display seconds

Use the Chinese date to display _ year and month string strYM = currentTime. ToString ("y ");

Display Chinese Date _ month/day string strMD = currentTime. ToString ("m ");

The format is 2003-9-23 string strYMD = currentTime. ToString ("d ");

The current time, in the format of 14: 24 string strT = currentTime. ToString ("t ");

DateTime. Now. ToString (); // obtain the complete date and time of the current system time.
DateTime. Now. ToLongDateString (); // only display the date xxxx-xx, and the other is a long date.
DateTime. Now. ToShortDateString (); // only the date xxxx-xx is a short date.

// Today DateTime. Now. Date. tow.datestring ();
// Yesterday's DateTime. Now. AddDays (-1). to1_datestring ();
// Tomorrow's DateTime. Now. AddDays (1). tow.datestring ();


// This Week (note that every week starts from Sunday to Saturday)
DateTime. Now. AddDays (Convert. ToDouble (0-Convert. ToInt16 (DateTime. Now. DayOfWeek). ToShortDateString ();
DateTime. Now. AddDays (Convert. ToDouble (6-Convert. ToInt16 (DateTime. Now. DayOfWeek). ToShortDateString ();

// Last week, last week is the week minus 7 days

DateTime. Now. AddDays (Convert. ToDouble (0-Convert. ToInt16 (DateTime. Now. DayOfWeek)-7). ToShortDateString ();
DateTime. Now. AddDays (Convert. ToDouble (6-Convert. ToInt16 (DateTime. Now. DayOfWeek)-7). ToShortDateString ();

// Add seven days to the week next week

DateTime. Now. AddDays (Convert. ToDouble (0-Convert. ToInt16 (DateTime. Now. DayOfWeek) + 7). ToShortDateString ();
DateTime. Now. AddDays (Convert. ToDouble (6-Convert. ToInt16 (DateTime. Now. DayOfWeek) + 7). ToShortDateString ();

// The first day of this month is the first day of the month, and the last day is the day after the first day of the month.

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 ). toShortDateString (); // last day

Another method:

DateTime now = DateTime. Now;
DateTime d1 = new DateTime (now. Year, now. Month, 1); // the first day of the Month

DateTime d2 = d1.AddMonths (1). AddDays (-1); // the last day of the month

PS:


DateTime. Now. DayOfWeek. ToString (); // weekly display in English, Wednesday

(Int) DateTime. Now. DayOfWeek number. If it is Wednesday, the result is 3.

DateTime. Now. ToString ("dddd", new System. Globalization. CultureInfo ("zh-cn"); // display the Chinese week
DateTime. Now. ToString ("dddd"); // display of Chinese week

DateTime. Now. ToString ("dddd, MMMM, dd, yyyy", new System. Globalization. DateTimeFormatInfo (); // display the date format Friday, July, 01,2009

DateTime. Now. ToString ("dddd, dd MMMM, yyyy") // output on Wednesday, December 30, January, 2008

Source: http://msdn.microsoft.com/zh-cn/vstudio/bb762911 (VS.95). aspx, how to: extract the day of the week from a specific date

Set the format of datetime type in tostring () format

Detailed format usage of the Parameter
Format character Association attributes/Description
D ShortDatePattern
D LongDatePattern
F complete date and time (long date and short time)
F FullDateTimePattern (long date and long time)
G regular (short date and short time)
G regular (short date and long time)
M, M MonthDayPattern
R, R RFC1123Pattern
S uses SortableDateTimePattern of the local time (based on ISO 8601)
T ShortTimePattern
T LongTimePattern
U UniversalSortableDateTimePattern is used to display the format of common time
U uses the complete date and time of the general time (long date and long time)
Y, Y YearMonthPattern

The following table lists the modes that can be merged to build a custom mode. These modes are case sensitive.

D. A single-digit date has no leading zero.
A day in dd month. A single-digit date has a leading zero.
The abbreviation of a day in ddd Week, which is defined in AbbreviatedDayNames.
The full name of the Day of the dddd Week, which is defined in DayNames.
M month number. One-digit month does not have a leading zero.
MM Month number. A single-digit month has a leading zero.
The abbreviated name of the MMM month, which is defined in AbbreviatedMonthNames.
The full name of the MMMM month, which is defined in MonthNames.
Y does not contain the year of the epoch. If the year that does not contain the epoch is less than 10, the year that does not have the leading zero is displayed.
Yy does not contain the year of the epoch. If the year that does not contain the epoch is less than 10, the year with the leading zero is displayed.
Yyyy includes the four-digit year of the epoch.
Gg period or epoch. This mode is ignored if the date to be formatted does not have an associated period or epoch string.

H 12-Hour. One-digit hours do not have a leading zero.
The hour in hh 12-hour format. The number of hours in a single digit has a leading zero.
H In 24-hour format. One-digit hours do not have a leading zero.
The hour in the 24-hour format. The number of hours in a single digit has a leading zero.

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.