DateTime.TryParse问题,日期为yyyy-dd-MM格式

Roc*_*ngh 73 c# asp.net datetime date tryparse

我有以下日期的字符串格式"2011-29-01 12:00 am".现在我尝试使用以下代码将其转换为datetime格式:

DateTime.TryParse(dateTime, out dt); 
Run Code Online (Sandbox Code Playgroud)

但我总是在{1/1/0001 12:00:00 AM}获得dt,你能告诉我为什么吗?以及如何将该字符串转换为日期.

编辑:我刚看到提到的每个人都使用格式参数.我现在要提到我不能使用format参数,因为我有一些设置来选择用户想要的自定义dateformat,并且基于该用户能够通过jQuery datepicker自动以该格式获取文本框中的日期.

Bro*_*ass 169

这应该基于您的示例"2011-29-01 12:00 am"

DateTime dt;
DateTime.TryParseExact(dateTime, 
                       "yyyy-dd-MM hh:mm tt", 
                       CultureInfo.InvariantCulture, 
                       DateTimeStyles.None, 
                       out dt);
Run Code Online (Sandbox Code Playgroud)

  • 要回答我自己的问题,在这种情况下,如果您使用格式中的单个字符,它适用于单字符和双字符日期.例如,d/m/yyyy适用于13/11/2012 (11认同)
  • 打败我.如果你知道输入字符串的格式,你几乎应该总是使用TryParseExact/ParseExact方法. (7认同)
  • @CiaranGallagher请稍作评论,您评论中的日期应使用大M(d / M / yyyy) (2认同)

Chr*_*isF 12

您需要使用该ParseExact方法.这会将字符串作为第二个参数,指定日期时间所在的格式,例如:

// Parse date and time with custom specifier.
dateString = "2011-29-01 12:00 am";
format = "yyyy-dd-MM h:mm tt";
try
{
   result = DateTime.ParseExact(dateString, format, provider);
   Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
}
catch (FormatException)
{
   Console.WriteLine("{0} is not in the correct format.", dateString);
}
Run Code Online (Sandbox Code Playgroud)

如果用户可以在UI中指定格式,则需要将其转换为可以传递给此方法的字符串.你可以通过允许用户直接输入格式字符串来做到这一点 - 虽然这意味着转换更可能失败,因为他们输入无效的格式字符串 - 或者有一个组合框,向他们提供可能的选择,而你设置这些选项的格式字符串.

如果输入可能不正确(例如用户输入),最好使用TryParseExact而不是使用异常来处理错误情况:

// Parse date and time with custom specifier.
dateString = "2011-29-01 12:00 am";
format = "yyyy-dd-MM h:mm tt";
DateTime result;
if (DateTime.TryParseExact(dateString, format, provider, DateTimeStyles.None, out result))
{
   Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
}
else
{
   Console.WriteLine("{0} is not in the correct format.", dateString);
}
Run Code Online (Sandbox Code Playgroud)

更好的选择可能是向用户提供日期格式选择,但使用带有一系列格式重载:

// A list of possible American date formats - swap M and d for European formats
string[] formats= {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt", 
                   "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss", 
                   "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt", 
                   "M/d/yyyy h:mm", "M/d/yyyy h:mm", 
                   "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm",
                   "MM/d/yyyy HH:mm:ss.ffffff" };
string dateString; // The string the date gets read into

try
{
    dateValue = DateTime.ParseExact(dateString, formats, 
                                    new CultureInfo("en-US"), 
                                    DateTimeStyles.None);
    Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue);
}
catch (FormatException)
{
    Console.WriteLine("Unable to convert '{0}' to a date.", dateString);
}                                               
Run Code Online (Sandbox Code Playgroud)

如果您从配置文件或数据库中读取可能的格式,则可以在遇到人们想要输入日期的所有不同方式时添加这些格式.


Kat*_*teA 6

尝试使用安全的 TryParseExact 方法

DateTime temp;
string   date = "2011-29-01 12:00 am";

DateTime.TryParseExact(date, "yyyy-dd-MM hh:mm tt", CultureInfo.InvariantCulture, DateTimeStyles.None, out temp);
Run Code Online (Sandbox Code Playgroud)