解析DateTime格式得到格式字符串

编程入门 行业动态 更新时间:2024-10-26 21:24:54
本文介绍了解析DateTime格式得到格式字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望能够从日期字符串得到的格式字符串

I would like to be able to get the format string from a DateTime string.

例如

2012年12月8日15时零零分00秒=>YYYY-MM-DD HH:MM:SS

"2012-12-08 15:00:00" => "yyyy-MM-dd HH:mm:ss"

2013/30/01 16:00=>YYYY / DD / MM HH:MM

"2013/30/01 16:00" => "yyyy/dd/MM HH:mm"

这可能吗?

推荐答案

这将是很难做到这一点在一个完全普遍的方​​式,但其中一个方案是提取相关 的DateTimeFormatInfo 请将CultureInfo.DateTimeFormat ),从( LongDatePattern , LongTimePattern 等),结合图案适当地在某些情况下(如 ShortDatePattern 空间 ShortTimePattern ),然后使用 DateTime.TryParseExact 依次尝试每个模式 - 记住每次还指定文化,以处理日期分隔等适当

It would be very hard to do this in a completely general way, but one option would be to extract the relevant DateTimeFormatInfo that you're interested in (using CultureInfo.DateTimeFormat), extract the culture-specific patterns from that (LongDatePattern, LongTimePattern etc), combine the patterns appropriately in some cases (e.g. ShortDatePattern space ShortTimePattern) and then try each pattern in turn using DateTime.TryParseExact - remembering to still specify the culture each time in order to handle date separators etc appropriately.

在 DateTime.TryParseExact 收益真正,你知道你已经得到的在的格局,这将解析给定文本

When DateTime.TryParseExact returns true, you know you've got a pattern which will parse the given text.

示例代码 - 包括显示在你期望它的工作,但它没有一个例子:

Sample code - including showing an example where you'd expect it to work but it doesn't:

using System; using System.Collections.Generic; using System.Globalization; class Test { static void Main() { var us = new CultureInfo("en-US"); var uk = new CultureInfo("en-GB"); string text = "07/06/2013 11:22:11"; // This one fails, as there's no appropriate time format Console.WriteLine(GuessPattern(text, us)); // This one prints dd/MM/yyyy HH:mm:ss Console.WriteLine(GuessPattern(text, uk)); } static string GuessPattern(string text, CultureInfo culture) { foreach (var pattern in GetDateTimePatterns(culture)) { DateTime ignored; if (DateTime.TryParseExact(text, pattern, culture, DateTimeStyles.None, out ignored)) { return pattern; } } return null; } static IList<string> GetDateTimePatterns(CultureInfo culture) { var info = culture.DateTimeFormat; return new string[] { info.FullDateTimePattern, info.LongDatePattern, info.LongTimePattern, info.ShortDatePattern, info.ShortTimePattern, info.MonthDayPattern, info.ShortDatePattern + " " + info.LongTimePattern, info.ShortDatePattern + " " + info.ShortTimePattern, info.YearMonthPattern // Consider the sortable pattern, ISO-8601 etc }; } }

您可能会硬编码了一些额外的日期以及你期望的工作时间格式

You could potentially hard-code some "extra" date and time formats which you expect to work.

编辑:要处理歧义,你可以很容易地使 GuessPattern 返回的IEnumerable<串> ,而不是一个字符串:

To handle ambiguity, you could easily make GuessPattern return an IEnumerable<string> instead of a single string:

static IEnumerable<string> GuessPatterns(string text, CultureInfo culture) { DateTime ignored; return GetDateTimePatterns(culture) .Where(pattern => DateTime.TryParseExact(text, pattern, culture, DateTimeStyles.None, out ignored)) } }

更多推荐

解析DateTime格式得到格式字符串

本文发布于:2023-11-22 15:21:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617953.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:格式   字符串   DateTime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!