Click here to Skip to main content
15,886,720 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi in my program i am converting the dates in string for my application purposes.I want to compare those strings to see if the dates have the appropriate difference between them.Example date1 = "07/02/2015 12:12:000" date2 = "08/02/2015 16:15:000" how can i compare for starters only the year value without parsing the string to date.Thanks in advance.
I have tried with .Length -something but with no luck at all.
Posted

1 solution

Don't.
Parse the strings to DateTime, and compare the year value from there.
Otherwise, you have to cope will all the possible variatiuons on a date format:
19/02/2014
19/02/14
14/02/19
2014/02/19
02/19/14
...
Are all valid versions of the same date in various cultures.
Using Parse allows the system to use the current culture information to start with for a "good guess" as to the actual date format.

If you must play with strings, then look at the String.Substring method to extract just the year - assuming it is always in the same place...
 
Share this answer
 
Comments
Member 10059109 19-Feb-15 8:38am    
It's always in the same place so you think i need to go with the substring method right?
OriginalGriff 19-Feb-15 10:18am    
You can...but I'd parse it.

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