c#datetime方法应用介绍
2023-06-13 09:14:41 时间
随着工作的需要,也算是写一个为自己留着的帮助文档吧。
System.DateTimecurrentTime=newSystem.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();//明天
中文日期年月日时分stringstrY=currentTime.ToString("f");//不显示秒
中文日期年月stringstrYM=currentTime.ToString("y");
中文日期月日stringstrMD=currentTime.ToString("m");
当前年月日格式为:2003-9-23stringstrYMD=currentTime.ToString("d");
当前时分格式为:14:24stringstrT=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(基于ISO8601)
t
ShortTimePattern
T
LongTimePattern
u
UniversalSortableDateTimePattern用于显示通用时间的格式
U
使用通用时间的完整日期和时间(长日期和长时间)
y、Y
y、YYearMonthPattern
附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小时制的小时。一位数的小时数有前导零。
System.DateTimecurrentTime=newSystem.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();//明天
中文日期年月日时分stringstrY=currentTime.ToString("f");//不显示秒
中文日期年月stringstrYM=currentTime.ToString("y");
中文日期月日stringstrMD=currentTime.ToString("m");
当前年月日格式为:2003-9-23stringstrYMD=currentTime.ToString("d");
当前时分格式为:14:24stringstrT=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的格式设置
附2:下表列出了可被合并以构造自定义模式的模式。这些模式是区分大小写的
相关文章
- C# ZIP文件的压缩和解压缩(SharpZipLib.dll)
- C#中HTML字符转换函数分享
- c#多图片上传并生成缩略图的实例代码
- C#获取打印机当前状态的方法
- C#使用FolderBrowserDialog类实现选择打开文件夹方法详解
- C#用户定义类型转换详解
- 深入理解C#中的枚举
- C#实现发送邮件的三种方法
- C#实现身份证号码验证的方法
- C#实现Xml序列化与反序列化的方法
- C#特性扩展方法
- 初步认识C#中的Lambda表达式和匿名方法
- C#与js实现去除textbox文本框里面重复记录的方法
- C#针对xml文件转化Dictionary的方法
- C#使用DataSetDatatable更新数据库的三种实现方法
- C#实现动态生成表格的方法