C Current System Time Acquisition and Time Format Detailed Explanation

  • 2021-11-13 18:00:06
  • OfStack

C # Current System Time Acquisition and Time Format

Recent learning C # knowledge, access to system time and time format summarized, this is the details of this article in the online collation, you see!


--DateTime 数字型  
System.DateTime currentTime=new System.DateTime();  
取当前年月日时分秒   currentTime=System.DateTime.Now;  
取当前年   int 年=currentTime.Year;  
取当前月   int 月=currentTime.Month;  
取当前日   int 日=currentTime.Day;  
取当前时   int 时=currentTime.Hour;  
取当前分   int 分=currentTime.Minute;  
取当前秒   int 秒=currentTime.Second;  
取当前毫秒  int 毫秒=currentTime.Millisecond; (变量可用中文) 
取中文日期显示――年月日时分  string strY=currentTime.ToString("f"); //不显示秒 
取中文日期显示_年月    string strYM=currentTime.ToString("y"); 
取中文日期显示_月日   string strMD=currentTime.ToString("m"); 
取当前年月日,格式为:2003-9-23   string strYMD=currentTime.ToString("d"); 
取当前时分,格式为:14:24   string strT=currentTime.ToString("t"); 
DateTime.Now.ToString();//获取当前系统时间 完整的日期和时间 
DateTime.Now.ToLongDateString();//只显示日期 xxxx年xx月xx日 ,1个是长日期 
DateTime.Now.ToShortDateString();//只显示日期 xxxx-xx-xx 1个是短日期 
//今天    DateTime.Now.Date.ToShortDateString(); 
//昨天 的    DateTime.Now.AddDays(-1).ToShortDateString(); 
//明天 的    DateTime.Now.AddDays(1).ToShortDateString(); 
 
//本周(注意这里的每1周是从周日始至周6止) 
DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString(); 
DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString(); 
//上周,上周就是本周再减去7天 
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(); 
//下周  本周再加上7天 
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(); 
//本月  本月的第1天是1号,最后1天就是下个月1号再减1天。 
DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1"; //第1天 
DateTime.Parse(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1").AddMonths(1).AddDays(-1).ToShortDateString();//最后1天 
另1种方法: 
DateTime now = DateTime.Now;  
DateTime d1 = new DateTime(now.Year, now.Month, 1); //本月第1天 
DateTime d2 = d1.AddMonths(1).AddDays(-1); //本月最后1天 
PS: 
DateTime.Now.DayOfWeek.ToString();//英文星期显示,Wednesday 
(int)DateTime.Now.DayOfWeek   数字,若是周3,结果对应为3 
DateTime.Now.ToString("dddd", new System.Globalization.CultureInfo("zh-cn")); //中文星期显示 
DateTime.Now.ToString("dddd");//中文星期显示 
DateTime.Now.ToString("dddd,MMMM,dd ,yyyy", new System.Globalization.DateTimeFormatInfo());//显示日期格式Friday,July, 01,2009 
DateTime.Now.ToString("dddd,dd MMMM,yyyy") //输出  星期3,30 1月,2008 
出处:http://msdn.microsoft.com/zh-cn/vstudio/bb762911(VS.95).aspx,如何:从特定日期中提取星期几 

Formatting of datetime type in tostring () format

Parameter format Format Detailed Usage
Format character association attribute/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 uses local time SortableDateTimePattern (based on ISO 8601)
t ShortTimePattern
T LongTimePattern
u UniversalSortableDateTimePattern Used to Display Universal Time Format
U uses the full date and time of Universal Time (long date and long time)
y, Y YearMonthPattern

The following table lists the schemas that can be merged to construct custom schemas. These patterns are case sensitive

d Day of the month. A 1-digit date has no leading zero.
dd One day of the month. A 1-digit date has 1 leading zero.
The abbreviated name of the day of the week in ddd, as defined in AbbreviatedDayNames.
The full name of the day of the dddd week, as defined in DayNames.
M monthly figures. A 1-digit month has no leading zero.
MM monthly figures. A 1-digit month has a leading zero.
The abbreviated name of the month of MMM, as 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 without era is less than 10, the year without leading zero is displayed.
yy does not contain the year of the era. If the year without era is less than 10, the year with leading zero is displayed.
yyyy includes the 4-digit year of the era.
gg period or epoch. If the date to be formatted does not have an associated period or era string, the pattern is ignored.
h 12-hour system hours. 1-digit hours have no leading zero.
hh 12-hour system hours. 1-digit hours have leading zeros.
H 24-hour system hours. 1-digit hours have no leading zero.
HH 24-hour system hours. 1-digit hours have leading zeros.

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: