当前位置: 移动技术网 > IT编程>开发语言>c# > c# datetime方法应用介绍

c# datetime方法应用介绍

2019年07月18日  | 移动技术网IT编程  | 我要评论
随着工作的需要,也算是写一个为自己留着的帮助文档吧。
system.datetime currenttime=new system.datetime(); //实例化一个 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; (变量可用中文)
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();//明天 
中文日期 年月日时分 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");
更多格式看 附1、2。 

//本周 (注意这里的每一周是从周日始至周六止)
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();//最后一天
--------------------------------------------------------------------------------
附1:datetime类型在tostring(),format的格式设置
格式字符  关联属性/说明
d   shortdatepattern
longdatepattern 
完整日期和时间(长日期和短时间)
fulldatetimepattern(长日期和长时间)
g   常规(短日期和短时间)
g   常规(短日期和长时间)
m、m   monthdaypattern
r、r   rfc1123pattern
使用当地时间的 sortabledatetimepattern(基于 iso 8601)
t   shorttimepattern
t   longtimepattern
u  universalsortabledatetimepattern 用于显示通用时间的格式
使用通用时间的完整日期和时间(长日期和长时间)
y、y  y、y yearmonthpattern

附2:下表列出了可被合并以构造自定义模式的模式。这些模式是区分大小写的
格式字符 关联属性/说明
月中的某一天。一位数的日期没有前导零。
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 小时制的小时。一位数的小时数有前导零。

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

相关文章:

验证码:
移动技术网