如何解析带有后缀“ th”,“ st”的日期?或“ nd”在一个月的一天? [英] How can I parse dates with a suffix "th", "st" or "nd" on the day of the month?

查看:254
本文介绍了如何解析带有后缀“ th”,“ st”的日期?或“ nd”在一个月的一天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 DateTime.Parse



时遇到麻烦

我正在处理各种日期格式并且其中某些格式的格式为 1月11日 2月22日,依此类推。



DateTime.Parse 抛出异常,试图解析这类日期。



我想知道我是否缺少DateTime中的内置功能,就像我可以设置的标志一样,它将使Parse的行为更容易接受。



我知道这可以用一个相对简单的正则表达式解决,而且我已经有了一个模糊匹配我编写的日期的类,但是我想知道是否

解决方案
解决方案

.net框架中没有任何内置内容可以解析 1月11日或2月22日等格式的日期。您必须删除后缀字符,然后您可以使用 DateTime.TryParseExact



对于带后缀 st th 的日期,您可以使用 string.Replace 删除该部分,然后使用 DateTime.TryParseExact 。喜欢。

  string str = 2013年2月1日; 
DateTime dtObject;
字符串替换后的Str = str.Substring(0,4)
.Replace( nd,)
.Replace( th,)
.Replace( rd,)
.Replace( st,)
+ str.Substring(4);


if(DateTime.TryParseExact(replacedStr,
dd MMMMM yyyy,
CultureInfo.InstalledUICulture,
DateTimeStyles.None,
out dtObject))
{
//有效日期
}

对于多种格式,您可以在字符串数组中指定格式,以后可以使用它。它返回一个 bool 值,指示解析是否成功。



来自MSDN的示例:

  string [] format = { 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}};
string [] dateStrings = { 2009年5月1日6:32 PM, 05/01/2009 6:32:05 PM,
2009年5月1日6:32: 00, 05/01/2009 06:32,
05/01/2009 06:32:00 PM, 05/01/2009 06:32:00}};
DateTime dateValue;

foreach(dateString中的字符串dateString)
{
if(DateTime.TryParseExact(dateString,format,
new CultureInfo( en-US)),
DateTimeStyles.None,
out dateValue))
Console.WriteLine(已将'{0}'转换为{1}。,dateString,dateValue);
else
Console.WriteLine(无法将 {0}转换为日期。,dateString);


I'm having trouble using DateTime.Parse

I'm handling a variety of formats of dates and some of which are of the formatJanuary 11th or February 22nd and so on.

DateTime.Parse throws an exception trying to parse these sort of dates.

I was wondering if there is a built in functionality in DateTime that I am missing, like a flag I can set that will make Parse behave in a more acceptable way.

I am aware that this is solvable with a relatively simple regular expression, moreover I already have a class that fuzzy matches dates that I wrote, however I would like to know if there is a built in way to perform this sort of extraction since it will more likely be easier to maintain in the long run than reinventing the wheel.

解决方案

There isn't anything inbuilt in .Net framework to parse dates in format of January 11th or February 22nd etc. You have to remove the suffix characters and then you can use DateTime.TryParseExact.

For dates with Suffix st, th, you can use string.Replace to remove that part and then use DateTime.TryParseExact. Like.

string str = "1st February 2013";
DateTime dtObject;
string replacedStr =  str.Substring(0,4)
                         .Replace("nd","")
                         .Replace("th","")
                         .Replace("rd","")
                         .Replace("st","")
                         + str.Substring(4);


if (DateTime.TryParseExact(replacedStr, 
                            "dd MMMMM yyyy", 
                            CultureInfo.InstalledUICulture, 
                            DateTimeStyles.None, 
                            out dtObject))
{
 //valid date
}

For multiple formats, you can specify the formats in a string array and later you can use that. It returns a bool value indicating if the parsing was successful or not.

Example from MSDN:

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"};
string[] dateStrings = {"5/1/2009 6:32 PM", "05/01/2009 6:32:05 PM", 
                        "5/1/2009 6:32:00", "05/01/2009 06:32", 
                        "05/01/2009 06:32:00 PM", "05/01/2009 06:32:00"}; 
DateTime dateValue;

foreach (string dateString in dateStrings)
{
   if (DateTime.TryParseExact(dateString, formats, 
                              new CultureInfo("en-US"), 
                              DateTimeStyles.None, 
                              out dateValue))
      Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue);
   else
      Console.WriteLine("Unable to convert '{0}' to a date.", dateString);

这篇关于如何解析带有后缀“ th”,“ st”的日期?或“ nd”在一个月的一天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆