随着工作的需要,也算是写一个为自己留着的帮助文档吧。
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 |
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 |
y、y yearmonthpattern |
附2:下表列出了可被合并以构造自定义模式的模式。这些模式是区分大小写的
格式字符 |
关联属性/说明 |
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 小时制的小时。一位数的小时数有前导零。 |
如对本文有疑问,
点击进行留言回复!!
相关文章:
-
-
-
C#实现猜数字游戏
本文实例为大家分享了c#实现猜数字游戏具体代码,供大家参考,具体内容如下给定一个0-100的随机数字猜其大小题目样式:电脑产生一个0到100之间的随机数字,并且...
[阅读全文]
-
-
C# 可空类型的具体使用
在项目中我们经常会遇到可为空类型,那么到底什么是可为空类型呢?下面我们将从4个方面为大家剖析。1、可空类型基础知识顾名思义,可空类型指的就是某个对象类型可以为空...
[阅读全文]
-
-
-
C#实现猜数字小游戏
本文实例为大家分享了c#实现猜数字小游戏的具体代码,供大家参考,具体内容如下效果如图:代码:using system;using system.collecti...
[阅读全文]
-
-
C#实现简单俄罗斯方块
最近在看《.net游戏编程入门经典 c#篇》 第一章介绍了如何制作俄罗斯方块,自己试了试按照书上的步骤,可算是完成了。于是写下这篇文章留作纪念。1.类的设计在充...
[阅读全文]
-
网友评论