当前位置: 移动技术网 > IT编程>开发语言>c# > C# 当前系统时间获取及时间格式详解

C# 当前系统时间获取及时间格式详解

2019年07月18日  | 移动技术网IT编程  | 我要评论

c# 当前系统时间获取及时间格式

      最近学习c# 的知识,对获取系统时间和时间格式进行了总结,这是本文在网上整理的详细资料,大家看下!

--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日 ,一个是长日期 
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   数字,若是周三,结果对应为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的格式设置 

参数format格式详细用法  
 格式字符 关联属性/说明  
 d shortdatepattern  
 d longdatepattern  
 f 完整日期和时间(长日期和短时间)  
 f fulldatetimepattern(长日期和长时间)  
 g 常规(短日期和短时间)  
 g 常规(短日期和长时间)  
 m、m monthdaypattern  
 r、r rfc1123pattern  
 s 使用当地时间的 sortabledatetimepattern(基于 iso 8601)  
 t shorttimepattern  
 t longtimepattern  
 u universalsortabledatetimepattern 用于显示通用时间的格式  
 u 使用通用时间的完整日期和时间(长日期和长时间)  
 y、y yearmonthpattern 

下表列出了可被合并以构造自定义模式的模式。这些模式是区分大小写的  

   d 月中的某一天。一位数的日期没有前导零。  
 dd 月中的某一天。一位数的日期有一个前导零。  
 ddd 周中某天的缩写名称,在 abbreviateddaynames 中定义。  
 dddd 周中某天的完整名称,在 daynames 中定义。  
 m 月份数字。一位数的月份没有前导零。  
 mm 月份数字。一位数的月份有一个前导零。  
 mmm 月份的缩写名称,在 abbreviatedmonthnames 中定义。  
 mmmm 月份的完整名称,在 monthnames 中定义。  
 y 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示不具有前导零的年份。  
 yy 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示具有前导零的年份。  
 yyyy 包括纪元的四位数的年份。  
 gg 时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。 
   h 12 小时制的小时。一位数的小时数没有前导零。  
 hh 12 小时制的小时。一位数的小时数有前导零。  
 h 24 小时制的小时。一位数的小时数没有前导零。  
 hh 24 小时制的小时。一位数的小时数有前导零。 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网