C# 當前系統(tǒng)時間獲取及時間格式
最近學習C# 的知識,對獲取系統(tǒng)時間和時間格式進行了總結,這是本文在網(wǎng)上整理的詳細資料,大家看下!
--DateTime 數(shù)字型  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();//獲取當前系統(tǒng)時間 完整的日期和時間 DateTime.Now.ToLongDateString();//只顯示日期 xxxx年xx月xx日 ,一個是長日期 DateTime.Now.ToShortDateString();//只顯示日期 xxxx-xx-xx 一個是短日期 //今天    DateTime.Now.Date.ToShortDateString(); //昨天 的    DateTime.Now.AddDays(-1).ToShortDateString(); //明天 的    DateTime.Now.AddDays(1).ToShortDateString();  //本周(注意這里的每一周是從周日始至周六止) 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號,最后一天就是下個月一號再減一天。 DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1"; //第一天 DateTime.Parse(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1").AddMonths(1).AddDays(-1).ToShortDateString();//最后一天 另一種方法: DateTime now = DateTime.Now;  DateTime d1 = new DateTime(now.Year, now.Month, 1); //本月第一天 DateTime d2 = d1.AddMonths(1).AddDays(-1); //本月最后一天 PS: DateTime.Now.DayOfWeek.ToString();//英文星期顯示,Wednesday (int)DateTime.Now.DayOfWeek   數(shù)字,若是周三,結果對應為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") //輸出  星期三,30 一月,2008 出處:http://msdn.microsoft.com/zh-cn/vstudio/bb762911(VS.95).aspx,如何:從特定日期中提取星期幾 datetime類型在tostring()format的格式設置  
參數(shù)format格式詳細用法   
 格式字符 關聯(lián)屬性/說明   
 d ShortDatePattern   
 D LongDatePattern   
 f 完整日期和時間(長日期和短時間)   
 F FullDateTimePattern(長日期和長時間)   
 g 常規(guī)(短日期和短時間)   
 G 常規(guī)(短日期和長時間)   
 m、M MonthDayPattern   
 r、R RFC1123Pattern   
 s 使用當?shù)貢r間的 SortableDateTimePattern(基于 ISO 8601)   
 t ShortTimePattern   
 T LongTimePattern   
 u UniversalSortableDateTimePattern 用于顯示通用時間的格式   
 U 使用通用時間的完整日期和時間(長日期和長時間)   
 y、Y YearMonthPattern  
下表列出了可被合并以構造自定義模式的模式。這些模式是區(qū)分大小寫的  
   d 月中的某一天。一位數(shù)的日期沒有前導零。   
 dd 月中的某一天。一位數(shù)的日期有一個前導零。   
 ddd 周中某天的縮寫名稱,在 AbbreviatedDayNames 中定義。   
 dddd 周中某天的完整名稱,在 DayNames 中定義。   
 M 月份數(shù)字。一位數(shù)的月份沒有前導零。   
 MM 月份數(shù)字。一位數(shù)的月份有一個前導零。   
 MMM 月份的縮寫名稱,在 AbbreviatedMonthNames 中定義。   
 MMMM 月份的完整名稱,在 MonthNames 中定義。   
 y 不包含紀元的年份。如果不包含紀元的年份小于 10,則顯示不具有前導零的年份。   
 yy 不包含紀元的年份。如果不包含紀元的年份小于 10,則顯示具有前導零的年份。   
 yyyy 包括紀元的四位數(shù)的年份。   
 gg 時期或紀元。如果要設置格式的日期不具有關聯(lián)的時期或紀元字符串,則忽略該模式。  
   h 12 小時制的小時。一位數(shù)的小時數(shù)沒有前導零。   
 hh 12 小時制的小時。一位數(shù)的小時數(shù)有前導零。   
 H 24 小時制的小時。一位數(shù)的小時數(shù)沒有前導零。   
 HH 24 小時制的小時。一位數(shù)的小時數(shù)有前導零。 
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答