C#转向时间 - 转换错误

时间:2016-02-09 11:51:28

标签: c# datetime

我正在尝试将刻度转换为格式化的日期/时间值,但我注意到时间不正确。

在下面的示例中,numberOfTicks1的值小于numberOfTicks2。但是,当我创建它的格式化日期/时间值时,numberOfTicks2比numberOfTicks1少约50s ......

为什么会这样?我应该以不同的方式转换它们吗?

long numberOfTicks1 = 635904556104808905;
long numberOfTicks2 = 635904559203480591;

DateTime myDate1 = new DateTime(numberOfTicks1);
DateTime myDate2 = new DateTime(numberOfTicks2);

Console.WriteLine(myDate1.ToString("dd-MM-yyyy HH:MM:ss.fff"));
Console.WriteLine(myDate2.ToString("dd-MM-yyyy HH:MM:ss.fff"));

// Result
//07-02-2016 15:02:50.480
//07-02-2016 15:02:00.348

**更新**

 YYYY = four-digit year
 MM   = two-digit month (01=January, etc.)
 DD   = two-digit day of month (01 through 31)
 hh   = two digits of hour (00 through 23) (am/pm NOT allowed)
 mm   = two digits of minute (00 through 59)
 ss   = two digits of second (00 through 59)
 s    = one or more digits representing a decimal fraction of a second

来源:https://www.w3.org/TR/NOTE-datetime

1 个答案:

答案 0 :(得分:6)

强烈怀疑这是因为您使用MM specifier代替mm specifier分钟。

Console.WriteLine(myDate1.ToString("dd-MM-yyyy HH:mm:ss.fff"));
Console.WriteLine(myDate2.ToString("dd-MM-yyyy HH:mm:ss.fff"));

真正的结果应该是:

07-02-2016 15:26:50.480
07-02-2016 15:32:00.348