"景先生毕设|www.jxszl.com

C#DateTime类型详解

2023-09-12 15:40编辑: www.jxszl.com景先生毕设
                            日期DateTime类型

表示时间上的一刻,通常以日期和当天的时间表示。
示例:
Console.WriteLine(DateTime.Now);
DateTime date=new DateTime(2006,2,28,12,12,12);
Console.WriteLine(date);
Console.WriteLine(date.DayOfWeek);
Console.WriteLine(date.DayOfYear);

TimeSpan duration = new System.TimeSpan(36, 0, 0, 0);
DateTime jinian = date.Add(duration);
Console.WriteLine(jinian);

DateTime的格式化输出
参数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

用DateTime计算特定日期
DateTime.Now.ToString();//今天
 //7天后
DateTime.Now.AddDays(7).ToShortDateString();
//本月范围(本月第一天和最后一天)
DateTime.Now.ToString("yyyy-MM-01");
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).AddDays(-1).ToShortDateString();
 //上个月,减去一个月份(得到上个月第一天和最后一天)
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(-1).ToShortDateString();                
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();


原文链接:http://www.jxszl.com/biancheng/C/445954.html