Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have two date fields in excel field. I want to compare these two fields. But since the value of excel fields is string convert them into DateTime may cause problem. So is there any way to compare them? I think its not possible, even if it is it won't be 100 % fail safe. Its my hunch nothing much. Please let me know if there is a way
Posted

No, it's not really possible, and it certainly isn't "safe" - you really do want to convert them to a DateTime value first and then do the comparison.

It's all ok to do a string compare if the dates are in ISO format: yyyy-MM-dd
Because string comparisons stop at the first difference and use character comparisons to determine order. But...if one date is 2014-01-3 and the other is 2014-01-14 then a string comparison gets them in the wrong order.

You could manually process the string and do the comparison on the various parts - but then you are duplicating the work that converting it to a DateTime does anyway! :laugh:
 
Share this answer
 
Comments
Arjun Menon U.K 3-Apr-14 6:45am    
Hi ,

I can assure you that both string value will be in same format. But what i find nearly impossible is to find the format of the data. like its dd/mm/yyyy format or mm/dd/yyyy format. Culture info do play a role rt?
OriginalGriff 3-Apr-14 6:59am    
Yes - you could look at the current culture and work it out from that...but was the Excel file produced on that PC? Because you can't tell from just looking is 01-02-2014 is Jan or Feb...
If they are all the same, are you sure they aren't formatted in the Excel file? Personally, I always use CTRL+; in Excel to insert today, which inserts a date format value.
How are you reading the file? OleDB?
Arjun Menon U.K 3-Apr-14 7:08am    
Hi,
That's the problem bro. If i can't know the format of date how on earth i can compare two dates. Like you said 01-02-2014 can be either Feb 1st 2014 or Jan 2nd 2014. How can i get the current culture?It's merely a string value. The data in excel is exported from TFS server, a work item like task,bug,user story anything like that
OriginalGriff 3-Apr-14 7:16am    
Depends - I assume that you are using OleDB to access the file? If so, I know of no way to get the culture directly (you can get it - I think, I haven't tried - with Excel Interop apparently:
http://www.breezetree.com/blog/index.php/common-mistakes-programming-excel-with-c-sharp/
Let me know if it works!)
The other solution is to assume "month first" and look at all rows to see if they work. If they don't then try "day first" and check that. Nasty - but there is no real solution if you can't tell the culture that the file was written in.
Arjun Menon U.K 3-Apr-14 8:34am    
mmm lemme check. Thanks anyways
If you have Data format on the fields you want to compare.
You can know for sure the field will be in a datetime format.

for more info on data format of fields/rows/columns: format-a-date in excel[^]

then you could do DateTime.Parse(your field).

When you have two instances of DateTime structure you can use operators "==", "!=" and ">", "<", ">=", "<=" in the sense "later", "earlier", "not earlier", "not later", respectively. Also, you can use subtraction operator ("-") which returns the result in the form of System.TimeSpan. This is a comprehensive set of operators to cover all your comparison needs.
 
Share this answer
 
v2
Comments
Arjun Menon U.K 3-Apr-14 6:47am    
that's the problem sky, i can't specify the format as sometimes the date will be in date/month/year and sometimes month/date/year :'(
BELGIUMsky 3-Apr-14 7:01am    
and can you just specify that the data will be a datetime
(don't know if its possible in excel)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900