总目录
C# 语法总目录
.net 框架基础 二 日期和时间
- 日期和时间
- 1. TimeSpan
- 2. DateTime
日期和时间
System空间下有三个不可变的结构体进行事件表示:DateTime、DateTimeOffset、TimeSpan
1. TimeSpan
创建TimeSpan方法:
- 通过构造器
- 通过调用其中一个静态 From … 方法
- 通过两个 DateTime相减得到
Console.WriteLine(new TimeSpan(2,30,0)); //02:30:00
Console.WriteLine(TimeSpan.FromHours(2.5)); //02:30:00
Console.WriteLine(TimeSpan.FromHours(-2.5)); //-02:30:00TimeSpan TheDays = TimeSpan.FromDays(10) - TimeSpan.FromSeconds(1);
Console.WriteLine(TheDays.Days); //9
Console.WriteLine(TheDays.Hours); //23
Console.WriteLine(TheDays.Minutes); //23
Console.WriteLine(TheDays.Seconds); //59
Console.WriteLine(TheDays.Milliseconds); //0Console.WriteLine(TheDays.TotalDays); //9.999988425925926
Console.WriteLine(TheDays.TotalHours); //239.99972222222223
Console.WriteLine(TheDays.TotalMinutes); //14399.983333333334
Console.WriteLine(TheDays.TotalSeconds); //863999
Console.WriteLine(TheDays.TotalMilliseconds); //863999000
2. DateTime
Console.WriteLine(DateTime.Now); //2024/5/9 10:28:49
Console.WriteLine(DateTimeOffset.Now); //2024/5/9 10:28:49 +08:00Console.WriteLine(DateTime.UtcNow); //2024/5/9 2:29:40 UTC标准时间
//格式化
string localDate = $"{DateTime.Now.Year}年{DateTime.Now.Month}月{DateTime.Now.Day}日 {DateTime.Now.Hour}:{DateTime.Now.Minute}:{DateTime.Now.Second}";
Console.WriteLine(localDate); //2024年5月9日 10:33:21
总目录
C# 语法总目录