如何在C#中比较两个日期 [英] How to compare two date in C#

查看:191
本文介绍了如何在C#中比较两个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友,

如何比较两个日期,找出天数的差异

09/10/18和10/03/18



问候,



我尝试过:



Hi Friends,
How to compare two dates and find the difference in number of days
09/10/18 and 10/03/18

Regards,

What I have tried:




使用日期函数


using date function

推荐答案

DateTime dt1 = new DateTime (2018, 10, 9);
DateTime dt2 = new DateTime (2018, 3, 10);
Timespan diff = dt1 - dt2;
Console.WriteLine(diff.TotalDays);


你可能对GitHub项目感兴趣,调用 Humanizer [ ^ ]



You may be interesting in GitHub project, callled Humanizer[^]

DateTime.UtcNow.AddHours(-30).Humanize() // returns: "yesterday"
TimeSpan.FromMilliseconds(3603001).Humanize(3) // returns" "1 hour, 3 seconds, 1 millisecond"
TimeSpan.FromDays(486).Humanize(maxUnit: TimeUnit.Year, precision: 7) // returms: "1 year, 3 months, 29 days" 


访问本文

这篇关于如何在C#中比较两个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆