简体   繁体   English

使用DateTime.ParseExact时,字符串未被识别为有效的DateTime

[英]String was not recognized as a valid DateTime when using DateTime.ParseExact

I have below piece of code to log the message. 我有下面的代码来记录消息。 Since I wanted to have the log for each date I tried to retrieve current date and then tried to create log file with that particular date with format path/dd_mm_yyyy_LogFile.txt . 由于我想获取每个date的日志,因此尝试检索当前date ,然后尝试使用path/dd_mm_yyyy_LogFile.txt格式创建具有该特定日期的日志文件。 Before that I had to retrieve current date without time. 在此之前,我不得不没有时间来获取当前date

StreamWrite sw=null;
var d = Convert.ToString(DateTime.Today.ToShortDateString());
var date = DateTime.ParseExact(d, "dd_MM_yyyy", CultureInfo.InvariantCulture);
//Error in the above line  
sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\" + d + "_LogFile.txt", true);
sw.WriteLine(DateTime.Now.ToString() + ": " + message);

But am getting String was not recognized as a valid DateTime . 但是获取String未被识别为有效的DateTime I followed many other posts like changing the "dd_MM_yyyy" to "dd-MM-yyy" or to "dm-yyyy" but unfortunately am still hitting the same error. 我关注了许多其他帖子,例如将"dd_MM_yyyy"更改为"dd-MM-yyy""dm-yyyy"但不幸的是仍然遇到相同的错误。 What else am missing here? 这里还缺少什么? Below screenshot for reference. 下面的截图供参考。 If you see the screenshot, I've proper d value fetched. 如果您看到屏幕截图,则说明已获取适当的d值。 But still the above exception. 但还是以上例外。

在此处输入图片说明

As I can see from the picture, you actually want "M/d/yyyy" format: 从图片中可以看到,您实际上需要"M/d/yyyy"格式:

  String d = @"2/26/2016"; // d's value has been taken from the screenshot
  DateTime date = DateTime.ParseExact(d, "M/d/yyyy", CultureInfo.InvariantCulture);

Your format string in Parse method should exactly match the one produced by ToShortDateString . 您在Parse方法中使用的格式字符串应与ToShortDateString生成的格式字符串完全匹配。 eg this works with me: 例如,这与我一起工作:

var d = Convert.ToString(DateTime.Today.ToShortDateString());
Console.WriteLine(d);

var date = DateTime.ParseExact(d, @"MM/dd/yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(date);

output: 输出:

02/26/2016                                                                                                                                                                                                                                             
02/26/2016 00:00:00

Look at the screen shot you posted. 查看您发布的屏幕截图。 The runtime value of the string is: 字符串的运行时值为:

"2/26/2016"

So the format string should be: 因此,格式字符串应为:

"M/dd/yyyy"

or: 要么:

"MM/dd/yyyy"

By using those other format strings, you're explicitly telling the system to use that exact format. 通过使用其他格式字符串,您可以明确地告诉系统使用该确切格式。 And the string you have doesn't match that format. 而且您的字符串与该格式不匹配。 Hence the error. 因此,错误。

Create d like this instead: 像这样创建d

var d = DateTime.Today.ToString("dd_MM_yyyy");

ToShortDateString() does not have the format you want. ToShortDateString()没有所需的格式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在DateTime.ParseExact上未将字符串识别为有效的DateTime - String was not recognized as a valid DateTime on DateTime.ParseExact DateTime.ParseExact字符串未被识别为有效的日期时间 - DateTime.ParseExact String not recognized as valid datetime DateTime.ParseExact:字符串未被识别为有效的DateTime - DateTime.ParseExact: String was not recognized as a valid DateTime DateTime.ParseExact()-无法将字符串识别为有效的DateTime - DateTime.ParseExact() - String was not recognized as a valid DateTime 使用DateTime.ParseExact C#无法将字符串识别为有效的DateTime - String was not recognized as a valid DateTime using DateTime.ParseExact C# 无法将字符串识别为DateTime.ParseExact的有效参数 - String was not recognized as a valid parameter for DateTime.ParseExact DateTime.ParseExact FormatException字符串未被识别为有效的DateTime - DateTime.ParseExact FormatException String was not recognized as a valid DateTime DateTime.ParseExact给出错误:字符串未被识别为有效的DateTime - DateTime.ParseExact give the error: String was not recognized as a valid DateTime DateTime.ParseExact引发以下错误“字符串未被识别为有效的DateTime” - DateTime.ParseExact is raising the following error “String was not recognized as a valid DateTime” Datetime.ParseExact“字符串未被识别为有效的日期时间”错误 - Datetime.ParseExact “String was not recognized as a valid DateTime” error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM