计算两个日期之间的数字(C#,CultureInfo) [英] Count number between two date (C#, CultureInfo)

查看:95
本文介绍了计算两个日期之间的数字(C#,CultureInfo)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取两个日期之间的数字:

DateTime base;
....
base = DateTime.ParseExact(pos[13], "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
//example base =  2014-06-21 17:00:00

DateTime col_N;

//then some for loop
col_N = DateTime.ParseExact(pos[k], "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture); 
//example col_N = 2014-06-22 00:00:00

要在两天之间做些事情,如下所示:

To get days between I'm doing like below:

int date_diff = (col_N - base).Days;

但它会返回我 0 .

此外,当我检查时:

string diff_dat2 = (col_N - base).TotalDays.ToString();

我得到了 0,291666666666667 .

如何更正它以获得 1 天?

How to correct it to get 1 day ?

推荐答案

我不确定您要实现的目标到底是什么,但我想您希望仅在不包括时间的日期中获得不同的天数.

I am not sure what exactly you are trying to achieve but I assume you want to get difference in days in dates only excluding the time.

要实现此目的,您需要执行.Date来删除DateTime的时间部分:

To achieve this you need to remove the time component of DateTime by doing .Date:

int date_diff = (col_N.Date - base.Date).Days;

在您的示例中,TotalDays是正确的,因为两次之间没有完整的日期,但是如果仅使用日期进行操作,您将得到正确的答案.

TotalDays is correct in your example as there is no full date between the two times that you have, but if you operate only in dates, you will get your answer just right.

这篇关于计算两个日期之间的数字(C#,CultureInfo)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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