C#获取当前系统时间

来源:互联网 发布:王思聪游戏水平 知乎 编辑:程序博客网 时间:2024/04/29 01:49

 

[c-sharp] view plaincopyprint?
  1. --DateTime 数字型   
  2. System.DateTime currentTime=new System.DateTime();   
  3. 取当前年月日时分秒      currentTime=System.DateTime.Now;   
  4. 取当前年     int 年=currentTime.Year;   
  5. 取当前月     int 月=currentTime.Month;   
  6. 取当前日     int 日=currentTime.Day;   
  7. 取当前时     int 时=currentTime.Hour;   
  8. 取当前分     int 分=currentTime.Minute;   
  9. 取当前秒     int 秒=currentTime.Second;   
  10. 取当前毫秒    int 毫秒=currentTime.Millisecond; (变量可用中文)  
  11. 取中文日期显示——年月日时分    string strY=currentTime.ToString("f"); //不显示秒  
  12. 取中文日期显示_年月       string strYM=currentTime.ToString("y");  
  13. 取中文日期显示_月日     string strMD=currentTime.ToString("m");  
  14. 取当前年月日,格式为:2003-9-23      string strYMD=currentTime.ToString("d");  
  15. 取当前时分,格式为:14:24      string strT=currentTime.ToString("t");  
  16. DateTime.Now.ToString();//获取当前系统时间 完整的日期和时间  
  17. DateTime.Now.ToLongDateString();//只显示日期 xxxx年xx月xx日 ,一个是长日期  
  18. DateTime.Now.ToShortDateString();//只显示日期 xxxx-xx-xx 一个是短日期  
  19. //今天        DateTime.Now.Date.ToShortDateString();  
  20. //昨天 的       DateTime.Now.AddDays(-1).ToShortDateString();  
  21. //明天 的       DateTime.Now.AddDays(1).ToShortDateString();  
  22.   
  23. //本周(注意这里的每一周是从周日始至周六止)  
  24. DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();  
  25. DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();  
  26. //上周,上周就是本周再减去7天  
  27. DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();  
  28. DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();  
  29. //下周    本周再加上7天  
  30. DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();  
  31.    DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();  
  32. //本月    本月的第一天是1号,最后一天就是下个月一号再减一天。  
  33. DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1"//第一天  
  34. DateTime.Parse(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1").AddMonths(1).AddDays(-1).ToShortDateString();//最后一天  
  35. 另一种方法:  
  36. DateTime now = DateTime.Now;   
  37. DateTime d1 = new DateTime(now.Year, now.Month, 1); //本月第一天  
  38. DateTime d2 = d1.AddMonths(1).AddDays(-1); //本月最后一天  
  39. PS:  
  40. DateTime.Now.DayOfWeek.ToString();//英文星期显示,Wednesday  
  41. int)DateTime.Now.DayOfWeek     数字,若是周三,结果对应为3  
  42. DateTime.Now.ToString("dddd"new System.Globalization.CultureInfo("zh-cn")); //中文星期显示  
  43. DateTime.Now.ToString("dddd");//中文星期显示  
  44. DateTime.Now.ToString("dddd,MMMM,dd ,yyyy"new System.Globalization.DateTimeFormatInfo());//显示日期格式Friday,July, 01,2009  
  45. DateTime.Now.ToString("dddd,dd MMMM,yyyy"//输出   星期三,30 一月,2008  
  46. 出处:http://msdn.microsoft.com/zh-cn/vstudio/bb762911(VS.95).aspx,如何:从特定日期中提取星期几  
  47. datetime类型在tostring()format的格式设置  
  48. 参数format格式详细用法   
  49.  格式字符 关联属性/说明   
  50.  d ShortDatePattern   
  51.  D LongDatePattern   
  52.  f 完整日期和时间(长日期和短时间)   
  53.  F FullDateTimePattern(长日期和长时间)   
  54.  g 常规(短日期和短时间)   
  55.  G 常规(短日期和长时间)   
  56.  m、M MonthDayPattern   
  57.  r、R RFC1123Pattern   
  58.  s 使用当地时间的 SortableDateTimePattern(基于 ISO 8601)   
  59.  t ShortTimePattern   
  60.  T LongTimePattern   
  61.  u UniversalSortableDateTimePattern 用于显示通用时间的格式   
  62.  U 使用通用时间的完整日期和时间(长日期和长时间)   
  63.  y、Y YearMonthPattern  
  64. 下表列出了可被合并以构造自定义模式的模式。这些模式是区分大小写的  
  65.    d 月中的某一天。一位数的日期没有前导零。   
  66.  dd 月中的某一天。一位数的日期有一个前导零。   
  67.  ddd 周中某天的缩写名称,在 AbbreviatedDayNames 中定义。   
  68.  dddd 周中某天的完整名称,在 DayNames 中定义。   
  69.  M 月份数字。一位数的月份没有前导零。   
  70.  MM 月份数字。一位数的月份有一个前导零。   
  71.  MMM 月份的缩写名称,在 AbbreviatedMonthNames 中定义。   
  72.  MMMM 月份的完整名称,在 MonthNames 中定义。   
  73.  y 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示不具有前导零的年份。   
  74.  yy 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示具有前导零的年份。   
  75.  yyyy 包括纪元的四位数的年份。   
  76.  gg 时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。  
  77.    h 12 小时制的小时。一位数的小时数没有前导零。   
  78.  hh 12 小时制的小时。一位数的小时数有前导零。   
  79.  H 24 小时制的小时。一位数的小时数没有前导零。   
  80.  HH 24 小时制的小时。一位数的小时数有前导零。  
 

摘自:http://hi.baidu.com/kenstime/blog/item/0a0248b1d3ab78acd9335a94.html

原创粉丝点击