5

I have these 2 string values:

  • Test1 = "2020-01-29T00:00:00Z"

  • Test2 = "29/01/2020 00:00:00"

and I am doing this comparison:

(DateTime.ParseExact(Test2.ToString(), "dd/M/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture).ToString("yyyy'-'MM'-'dd'T'00':'00':'00'Z'") != (DateTime.ParseExact(Test1["ProjectDateSinged"].ToString(), "yyyy'-'MM'-'dd'T'00':'00':'00'Z'", CultureInfo.InvariantCulture)).ToString()))

but this will raise the following exception:

Error “String was not recognized as a valid DateTime”

Could anyone find what is wrong with my code?

LopDev
  • 823
  • 10
  • 26
John John
  • 1
  • 72
  • 238
  • 501

3 Answers3

4

Expanding my comment into answer, you should update your format string a little bit. For Test2 you should use dd/MM/yyyy hh:mm:ss format.

According to Custom date and time format strings MM is used for month number from 01 to 12, M from 1 to 12. You have 01 month number, so MM should be used.

There is also no AM/PM representation in your date, so tt is not needed as well

Them you'll be able to parse Test2 into the date.

var Test2 = "29/01/2020 00:00:00";
var result = DateTime.ParseExact(Test2, "dd/MM/yyyy hh:mm:ss", CultureInfo.InvariantCulture);

For the Test1 you can use yyyy-MM-ddThh:mm:ssK (parse the date including time zone information) or yyyy-MM-ddThh:mm:ss'Z' without time zone information.

To compare the dates you don't need to convert them back to string. You can simply get the date component using Date property of DateTime struct. The code below returns true

var result = DateTime.ParseExact(Test1, "yyyy-MM-ddThh:mm:ss'Z'", CultureInfo.InvariantCulture).Date ==
             DateTime.ParseExact(Test2, "dd/MM/yyyy hh:mm:ss", CultureInfo.InvariantCulture).Date;

as well as this, by comparing two DateTime instances only

var result = DateTime.ParseExact(Test1, "yyyy-MM-ddThh:mm:ss'Z'", CultureInfo.InvariantCulture) ==
             DateTime.ParseExact(Test2, "dd/MM/yyyy hh:mm:ss", CultureInfo.InvariantCulture);
Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
  • i tried replacing M with MM but i am still getting the same error – John John Feb 24 '20 at 11:53
  • @testtest you should also remove `tt`, as it stated in the answer. Please, check it – Pavel Anikhouski Feb 24 '20 at 11:54
  • ok thanks but i am not sure why does this `(DateTime.ParseExact(Test1["ProjectDateSinged"].ToString(), "yyyy'-'MM'-'dd'T'00':'00':'00'Z'", CultureInfo.InvariantCulture)).ToString())` will return the date as follow `29/01/2020 00:00:00"` ?? – John John Feb 24 '20 at 12:11
  • @testtest because you skip the time component during parsing using `'` quotes. To get the correct date you need `yyyy-MM-ddThh:mm:ss'Z'` (omit time zone) or `yyyy-MM-ddThh:mm:ssK` (use timezone) – Pavel Anikhouski Feb 24 '20 at 12:12
  • i mean the `(DateTime.ParseExact(Test1["ProjectDateSinged"].ToString(), "yyyy'-'MM'-'dd'T'00':'00':'00'Z'", CultureInfo.InvariantCulture)).ToString())` should return the string as follow `2020-01-29T00:00:00z` and not as `29/01/2020 00:00:00`.. is this correct? – John John Feb 24 '20 at 12:14
  • @testtest it returns `1/29/2020 12:00:00 AM` (at my end a least). `z` is omitted during parsing. It isn't clear why do you need to convert the date to string back and compare. You can use `Date` property of date time to get a date component and compare it – Pavel Anikhouski Feb 24 '20 at 12:20
1
        string Test1 = "2020-01-29T00:00:00Z";

        string Test2 = "29/01/2020 00:00:00";


        DateTime dt = Convert.ToDateTime(Test1, CultureInfo.InvariantCulture);

       MessageBox.Show("" + dt.ToString("yyyy-MM-ddT00:00:00Z"));

Used this code then successfully work

Malakiya sanjay
  • 208
  • 2
  • 12
0

By looking at your examples, the formats looks below.. You can write a generalized method by specifying formats. something like below -

private static DateTime ParseDate(string providedDate)
{
    DateTime validDate;
    string[] formats = {  "dd/MM/yyyy hh:mm:ss", "yyyy-MM-dd'T'hh:mm:ss'Z'" };
    var dateFormatIsValid = DateTime.TryParseExact(
        providedDate,
        formats,
        CultureInfo.InvariantCulture,
        DateTimeStyles.None,
        out validDate);
    return dateFormatIsValid ? validDate : DateTime.MinValue;
}

And call this method to parse the string

 var test1 = ParseDate("2020-01-29T00:00:00Z");
 var test2 = ParseDate("29/01/2020 00:00:00");

 Console.WriteLine(test1 == test2); // result TRUE
Krishna Varma
  • 4,238
  • 2
  • 10
  • 25