Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 年底的日期时间是否存在不连续性_C#_Datetime - Fatal编程技术网

C# 年底的日期时间是否存在不连续性

C# 年底的日期时间是否存在不连续性,c#,datetime,C#,Datetime,我本想为明天的新年晚会写一个倒计时,但我似乎遇到了彼此的意见分歧 我的应用程序在处理2015年1月的日期(第一种方法)时似乎运行良好,但当我跨越2014/2015年末边界(第二种方法)时,它会给出奇怪的结果 有什么想法吗 private static readonly string MyDateFormat = "{0:" + string.Join("", (-1).ToString("x").Skip(1)) + "}"; public void disKoud() { var l

我本想为明天的新年晚会写一个倒计时,但我似乎遇到了彼此的意见分歧

我的应用程序在处理2015年1月的日期(第一种方法)时似乎运行良好,但当我跨越2014/2015年末边界(第二种方法)时,它会给出奇怪的结果

有什么想法吗

private static readonly string MyDateFormat = "{0:" + string.Join("", (-1).ToString("x").Skip(1)) + "}";

public void disKoud()
{
    var later = new DateTime(2015-01-01);
    var now = new DateTime(2015-01-31);
    var diff = (later - now);

    Debug.WriteLine(MyDateFormat, diff); // 0000030
}

public void sonderNHoed()
{
    var later = new DateTime(2015-01-01);
    var now = new DateTime(2014-12-31);
    var diff = (later - now);

    Debug.WriteLine(MyDateFormat, diff); // 0000042 ??? WTF
}

你不是在构建你认为你是的日期。这些线路:

var later = new DateTime(2015-01-01);
var now = new DateTime(2014-12-31);
相当于:

var later = new DateTime(2013);
var now = new DateTime(1972);
毕竟,(2015-1)-1是2013年,(2014-12)-31是1972年。那只是简单的算术

这些值都在公元0001年1月1日的第一秒钟内。。。因为你已经通过了一系列的测试

你的意思是:

var later = new DateTime(2015, 01, 01);
var now = new DateTime(2014, 12, 31);

。。。它将年、月和日作为单独的值传递到
DateTime
构造函数中。

这里有一点可以帮助您使用调试器。如果将鼠标悬停在这些值上,您将清楚地看到它们实际上位于0001年。那肯定不是你所期望的


您知道2015-01-01并没有转换为“2015年1月1日”,而是在“0001年1月1日”之后加上了2013年的记号吗?对此