解析日期一样" 13日2010年1月"使用.NET [英] Parsing a Date Like "Wednesday 13th January 2010" with .NET

查看:161
本文介绍了解析日期一样" 13日2010年1月"使用.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何转换以下字符串到System.DateTime的对象?

How can I convert the following strings to a System.DateTime object?

周三2010年1月13日结果
周四2010年1月21日结果
星期三2010年2月3日

Wednesday 13th January 2010
Thursday 21st January 2010
Wednesday 3rd February 2010

通常像下面这样会做

DateTime dt;
DateTime.TryParseExact(value, "dddd d MMMM yyyy", DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out dt);



但是,这并不因为'日'的工作,ST或RD字符串

but this doesn't work because of the 'th', 'st' or 'rd' in the string

更新

看来,日期时间的不支持的格式中的'日','圣','次'等因此他们需要分析之前被剥离。鲁本斯·法里亚斯提供以下一个不错的正则表达式。

It appears that DateTime doesn't support formatting the 'th', 'st', 'rd' etc so they need to be stripped before parsing. Rubens Farias provides a nice regular expression below.

推荐答案

怎么样带他们?

string value = "Wednesday 13th January 2010";
DateTime dt;
DateTime.TryParseExact(
    Regex.Replace(value, @"(\w+ \d+)\w+ (\w+ \d+)", "$1 $2"),
    "dddd d MMMM yyyy", 
    DateTimeFormatInfo.InvariantInfo, 
    DateTimeStyles.None, out dt);

这篇关于解析日期一样" 13日2010年1月"使用.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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