c# DateTime常用操作实例(datetime计算时间差)
DateTime是C#编程语言中用于处理日期和时间的内置类型,它提供了丰富的功能来创建、操作和格式化日期和时间。在本实例中,我们将深入探讨如何进行常见的DateTime操作,特别是计算时间差。 我们可以通过DateTime.Now属性获取当前系统的日期和时间。例如: ```csharp Console.WriteLine("当前时间:{0}", DateTime.Now.ToString()); ``` 这里的`ToString()`方法允许我们自定义日期和时间的显示格式。默认情况下,它会以“年-月-日 时:分:秒”的形式输出。 如果只需要日期部分,可以使用`.Date`属性: ```csharp Console.WriteLine("日期部分:{0}", DateTime.Now.Date.ToString()); ``` 对于时间部分,我们可以使用`.ToLongTimeString()`: ```csharp Console.WriteLine("时间部分:{0}", DateTime.Now.ToLongTimeString()); ``` `TimeOfDay`属性返回一天中的时间,不包含日期,精确到毫秒: ```csharp Console.WriteLine("TimeOfDay:{0}", DateTime.Now.TimeOfDay.ToString()); ``` 此外,DateTime类还支持多种自定义格式化输出,如中文日期显示: ```csharp Console.WriteLine("取中文日期显示_年月日时分:{0}", DateTime.Now.ToString("f")); Console.WriteLine("取中文日期显示_年月:{0}", DateTime.Now.ToString("y")); Console.WriteLine("取中文日期显示_月日:{0}", DateTime.Now.ToString("m")); Console.WriteLine("取中文年月日:{0}", DateTime.Now.ToString("D")); ``` 还有其他格式,如24小时制的时分表示、标准ISO8601格式、UTC格式等: ```csharp // ... Console.WriteLine("取当前时分:{0}", DateTime.Now.ToString("t")); Console.WriteLine("取当前时分:{0}", DateTime.Now.ToString("s")); Console.WriteLine("取当前时分:{0}", DateTime.Now.ToString("u")); Console.WriteLine("取当前时分:{0}", DateTime.Now.ToString("g")); Console.WriteLine("取当前时分:{0}", DateTime.Now.ToString("r")); // ... ``` 除了显示日期和时间,DateTime还支持算术运算,比如添加或减去一定的时间间隔。例如,增加100天: ```csharp DateTime newDay = DateTime.Now.AddDays(100); Console.WriteLine(newDay.ToString()); ``` 我们还可以单独获取年、月、日、小时、分钟等组件: ```csharp Console.WriteLine("年:{0}", DateTime.Now.Year.ToString()); Console.WriteLine("月:{0}", DateTime.Now.Month.ToString()); Console.WriteLine("日:{0}", DateTime.Now.Day.ToString()); Console.WriteLine("时:{0}", DateTime.Now.Hour.ToString()); Console.WriteLine("分:{0}", DateTime.Now.Minute.ToString()); ``` 在计算时间差时,可以使用`Subtract`方法,它会返回一个`TimeSpan`对象,表示两个DateTime值之间的差值: ```csharp DateTime dateTime1 = new DateTime(2020, 1, 1); DateTime dateTime2 = new DateTime(2021, 1, 1); TimeSpan timeDifference = dateTime2.Subtract(dateTime1); Console.WriteLine("时间差:{0} 天", timeDifference.TotalDays); ``` 总结来说,C#中的DateTime类提供了广泛的功能,包括获取当前时间、格式化输出、日期时间算术运算以及计算时间差。通过熟练掌握这些操作,开发者可以轻松地处理各种日期和时间相关的任务。
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![thumb](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/release/download_crawler_static/12812493/bg1.jpg)
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/676103cba1ad4056a6ee10a1b22ced8d_weixin_38684328.jpg!1)
- 粉丝: 5
- 资源: 898
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)