Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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# DateTime.ParseExact无法在c中正确分析日期#_C#_Date_Datetime - Fatal编程技术网

C# DateTime.ParseExact无法在c中正确分析日期#

C# DateTime.ParseExact无法在c中正确分析日期#,c#,date,datetime,C#,Date,Datetime,我有一个yyyy-MM-dd hh:MM:ss tt格式的字符串日期,我想将其转换为具有相同格式的日期时间 下面是c#中的代码 GetNextValidDate返回一个字符串“2016-03-23 01:00:00 PM”,我想将其转换为具有相同格式的日期时间 如何做到这一点?在渲染到日期时,请尝试以下方法: foreach(DateTime d in dates) { string DateString = d.ToString("yyyy-MM-dd hh:mm:ss");

我有一个yyyy-MM-dd hh:MM:ss tt格式的字符串日期,我想将其转换为具有相同格式的日期时间

下面是c#中的代码

GetNextValidDate返回一个字符串“2016-03-23 01:00:00 PM”,我想将其转换为具有相同格式的日期时间


如何做到这一点?

在渲染到日期时,请尝试以下方法:

foreach(DateTime d in dates)
{
    string DateString = d.ToString("yyyy-MM-dd hh:mm:ss");

    //Do something with the string called, DateString

}

“不工作”怎么说?您是否收到异常?否它返回的日期格式错误。我认为您将调试器显示的值与实际存储的值混淆了。在内部,
DateTime
只是一个数字。您可以使用多种不同的方式对其进行格式化。为了方便起见,调试器恰好以某种方式显示它。如果要格式化它,请使用适当的
ToString()
参数将其输出为字符串。不应该
GetNextValidDate
返回
DateTime
DateTime
没有特定的格式
foreach(DateTime d in dates)
{
    string DateString = d.ToString("yyyy-MM-dd hh:mm:ss");

    //Do something with the string called, DateString

}