在我的 c# 程序中,用户可以从 winforms 日期时间选择器中选择一个日期。这被命名为dtpOutbox
。
这是我的代码:
DateTime selectedDate = dtpOutbox.Value.Date;
TimeSpan ts = new TimeSpan(23,59,59);
DateTime endOfSelectedDay = selectedDate + ts;
Console.WriteLine(selectedDate.ToString("yyyy-MM-dd hh:mm:ss"));
Console.WriteLine(endOfSelectedDay.ToString("yyyy-MM-dd hh:mm:ss"));
现在,如果今天的日期是 2 月 22 日,我的理解是这应该输出(期望的输出):
2013-02-22 00:00:00
2013-02-22 23:59:59
但这种情况并非如此。我的输出是:
2013-02-22 12:00:00
2013-02-22 11:59:59
我很确定这是一个格式问题,因为当我显示没有格式字符串的 DateTime 值 (selectedDate.ToString()) 时,它会正确显示 22/02/2013 00:00:00。但是,我需要格式为 22-02-2013 00:00:00 的日期。
任何想法为什么 C# 决定我的第一次约会将是中午,而我的第二次约会将是 11:59:59?