我有以下内容:
string QDI_DATE_FORMAT = "yyyy-MM-ddTHH:mm:00.0000000K";
string from = "2016-06-20T16:20:00.0000000-04:00";
string to = "2016-06-21T16:21:00.0000000-04:00";
DateTime fromDate = DateTime.ParseExact(from, QDI_DATE_FORMAT, CultureInfo.InvariantCulture, DateTimeStyles.None).Date;
DateTime toDate = DateTime.ParseExact(to, QDI_DATE_FORMAT, CultureInfo.InvariantCulture, DateTimeStyles.None).Date;
Console.WriteLine(fromDate);
Console.WriteLine(toDate);
这会打印出没有小时和分钟的日期。如何使其工作并显示时间?
答案 0 :(得分:2)
通过使用.Date
,您只能从生成的DateTime对象中选择日期部分。因此,将应用时间的默认值,删除.Date
,然后您将获得预期结果;
DateTime fromDate = DateTime.ParseExact(from, QDI_DATE_FORMAT, CultureInfo.InvariantCulture, DateTimeStyles.None);
DateTime toDate = DateTime.ParseExact(to, QDI_DATE_FORMAT, CultureInfo.InvariantCulture, DateTimeStyles.None);
此Example会显示差异
答案 1 :(得分:1)
您正在调用.date,它只选择日期部分:
string from = "2016-06-20T16:20:00.0000000-04:00";
DateTime fromDateTime = DateTime.ParseExact(from, QDI_DATE_FORMAT, CultureInfo.InvariantCulture, DateTimeStyles.None);
DateTime toDateTime = DateTime.ParseExact(to, QDI_DATE_FORMAT, CultureInfo.InvariantCulture, DateTimeStyles.None);
将产生:
> 6/20/2016 8:20:00 PM
> 6/21/2016 8:21:00 PM
注意最后缺少.Date
答案 2 :(得分:0)
你要做的就是删除你的结尾"。日期"来自你的代码。
string QDI_DATE_FORMAT = "yyyy-MM-ddTHH:mm:00.0000000K";
string from = "2016-06-20T16:20:00.0000000-04:00";
string to = "2016-06-21T16:21:00.0000000-04:00";
DateTime fromDate = DateTime.ParseExact(from, QDI_DATE_FORMAT, CultureInfo.InvariantCulture, DateTimeStyles.None);
DateTime toDate = DateTime.ParseExact(to, QDI_DATE_FORMAT, CultureInfo.InvariantCulture, DateTimeStyles.None);
Console.WriteLine(fromDate);
Console.WriteLine(toDate);
Console.ReadLine();