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);