Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

Count number between two date (C#, CultureInfo)

I trying to get Number between 2 dates:

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;

but it return me 0.

Also, when I checked:

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

I got 0,291666666666667.

How to correct it to get 1 day ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...