我正在将C#的日期时间转换为字符串。稍后当我将其转换回DateTime对象时,发现它们不相等。
我将C#的日期时间转换为字符串,但是当我尝试将其转换回DateTime对象时,它们似乎不相等。
const string FMT = "yyyy-MM-dd HH:mm:ss.fff";
DateTime now1 = DateTime.Now;
string strDate = now1.ToString(FMT);
DateTime now2 = DateTime.ParseExact(strDate, FMT, CultureInfo.InvariantCulture);
Console.WriteLine(now1.ToBinary());
Console.WriteLine(now2.ToBinary());
这里是一个例子。看起来所有的东西都包含在字符格式中,当我打印日期时,两者显示相同,但当我比较对象或以二进制格式打印日期时,我会看到差异。对我来说这看起来很奇怪,请您解释一下这里发生了什么?
以下是上述代码的输出。
-8588633131198276118
634739049656490000
now1.ToString(FMT, CultureInfo.InvariantCulture);
- Dr. Wily's Apprentice