C# 两个日期之间的天数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11772498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:11:14  来源:igfitidea点击:

Number of days between two dates

c#date

提问by user1502952

I have two dates:

我有两个日期:

Jun 26 2012 12:13AM and Jul 31 2012 12:54PM

2012 年 6 月 26 日上午 12:13 和 2012 年 7 月 31 日下午 12:54

I need to compare this two dates and extract Number of days(difference) between them

我需要比较这两个日期并提取它们之间的天数(差异)

采纳答案by Habib

Use TimeSpan

使用时间跨度

TimeSpan difference = dateTime1 - dateTime2;

difference.TotalDayswill give you number of days

difference.TotalDays会给你天数

var days = difference.TotalDays;