-2

I have following strings:

string s1 = "dd/mm/yyyy hh:mm"; // 24H format
string s2 = "mm/dd/yyyy hh:mm:ss AM/PM" //12H format

I need to compare them in my test (it means that i need to convert s1 to s2 or viceversa). Please help!

  • 3
    Just parse both to `DateTime` and then compare. – juharr Oct 14 '14 at 19:42
  • 2
    Convert your Date strings to `DateTime` using their specific format. Please note that for month it is upper case `MM`, and for AM/PM its `tt`, also for Hour part in first Date String, it should be `HH` (upper case) for 24 hours format. See [Custom Date and Time Format Strings](http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx) – Habib Oct 14 '14 at 19:43

1 Answers1

4

Convert both to DateTime and then compare them:

var result = DateTime.ParseExact(dt1, "dd/MM/yyyy HH:mm", null) > DateTime.ParseExact(dt2, "MM/dd/yyyy hh:mm:ss tt", null);
brz
  • 5,926
  • 1
  • 18
  • 18