System time acquisition issues in C #

Source: Internet
Author: User
Tags iso 8601

C # get current system time

2010-01-02 16:24

--datetime Digital Type
System.DateTime currenttime=new System.DateTime ();

Take the current month and day time of the second currenttime=system.datetime.now;

take the current year int year =currenttime.year;
take the current month int month =currenttime.month;
take the current day int day =currenttime.day;
=currenttime.hour when the current int is taken;
take the current sub-int sub-=currenttime.minute;
takes the current second int seconds =currenttime.second;
take the current millisecond int milliseconds =currenttime.millisecond; (variable available in Chinese) Take Chinese date display--------------------------date stry=currenttime.tostring ("F"); Date string strym=currenttime.tostring ("Y");

Take Chinese date display _ Month Day string strmd=currenttime.tostring ("M"), take current month day, format: 2003-9-23 string strymd=currenttime.tostring ("D" ); Take the current hour, in the format: 14:24 string strt=currenttime.tostring ("T");D ateTime.Now.ToString ();//Get the current system time complete date and time
DateTime.Now.ToLongDateString ();//only display date xxxx year XX month XX day, one is long date
DateTime.Now.ToShortDateString ();//Only show date xxxx-xx-xx one is short date//Today DateTime.Now.Date.ToShortDateString ();
//Yesterday's DateTime.Now.AddDays (-1). ToShortDateString ();
//Tomorrow's DateTime.Now.AddDays (1). ToShortDateString ();
//This week (note that every week here is 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, it was this 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 ()///next week plus 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 ();//The first day of this month is number 1th, and the last day is the next month and one more day. DateTime.Now.Year.ToString () + DateTime.Now.Month.ToString () + "1"; 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 ();//English week, Wednesday (int) DateTime.Now.DayOfWeek number, if Wednesday, The results correspond to 3datetime.now.tostring ("dddd", New System.Globalization.CultureInfo ("ZH-CN")); Chinese weekday display
DateTime.Now.ToString ("dddd");//Chinese weekday display DateTime.Now.ToString ("Dddd,mmmm,dd, yyyy", new System.Globalization.DateTimeFormatInfo ());//Display date format friday,july, 01,2009datetime.now.tostring ("dddd,ddmmmm,yyyy" )//Output Wednesday, 301 months, 2008 Source: http://msdn.microsoft.com/zh-cn/vstudio/bb762911 (vs.95). aspx, How to: Extract Days of the week from a specific date datetime type format parameter in tostring () format verbose usage
Format Character Association properties/Description
d ShortDatePattern
D Longdatepattern
f Full Date and time (long date and short time)
F Fulldatetimepattern (long date and long time)
G General (short date and short time)
G General (short date and long time)
m, M Monthdaypattern
R, R Rfc1123pattern
s use local time Sortabledatetimepattern (based on ISO 8601)
T Shorttimepattern
T Longtimepattern
u universalsortabledatetimepattern format for displaying the Universal Time
U Use the full date and time of the universal Time (long date and long time)
y, y yearmonthpattern The following table lists the patterns that can be combined to construct a custom pattern. These patterns are case-sensitive for the day of the D month. One-digit date has no leading zeros.
DD a day of the month. A one-digit date has a leading zero.
the abbreviated name of the day of the DDD week, defined in Abbreviateddaynames.
dddd The full name of the day of the week, as defined in DayNames.
M-month number. One-digit month has no leading zeros.
MM Month number. One-digit month has a leading zero.
abbreviated name of the MMM month, defined in AbbreviatedMonthNames.
The full name of the MMMM month, as defined in MonthNames.
y does not contain the year of the era. If the year that does not contain an era is less than 10, the year is displayed without leading zeros.
yy does not contain the year of the era. If the year that does not contain an era is less than 10, the year with leading zeros is displayed.
The yyyy includes the four-digit year of the era.


GG period or ERA.   If the date to be formatted does not have an associated period or era string, the pattern is ignored. H 12 Hour hour system. One-digit hours do not have leading zeros.
HH 12-hour hour. One-digit hours have leading zeros.
H 24 hour hour system. One-digit hours do not have leading zeros.
HH 24-hour hour. One-digit hours have leading zeros.

System time acquisition issues in C #

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.