C# - 如何验证DateTime(使“20120713”或“120713”TO“13.07.2012”) [英] C# - How To Validate DateTime ( make "20120713" OR "120713" TO "13.07.2012" )

查看:102
本文介绍了C# - 如何验证DateTime(使“20120713”或“120713”TO“13.07.2012”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从EDI-Order(20120713/ YYYYMMDD或120713/ YYMMDD或甚至其他日期,没有点数,所以只是数字)解析一个日期时间,如DD.MM.YYY在C#中。

I'm trying to parse a DateTime from EDI-Order ( "20120713" / YYYYMMDD or "120713" / YYMMDD or even other dates WITHOUT dots, so just numbers ) to a valied Date like "DD.MM.YYY" in C#.

我不知道在不同的EDI订单中有多少不同的日期格式,所以我正在寻找一个可以应用于所有

I have no idea how many diferent date-formats are in the diferent EDI-Orders, so i'm searching for a solution i can apply to all of them.

感谢大家。

推荐答案

你应该感兴趣使用这个 ParseExact 的重载,你可以传递多个格式作为数组,它将尝试根据它们进行解析(如果你可以控制格式并打算使用一个进程)

You should be interested in using this overload of ParseExact, you can pass in multiple formats as an array and it will attempt to parse it based on them.(it would be good if you can control the formats and intend on using one for the process)

DateTime start = DateTime.ParseExact("20120713",
                    new[] { "yyyyMMdd", "yyMMdd" },
                    CultureInfo.InvariantCulture,
                    DateTimeStyles.None);
DateTime end = DateTime.ParseExact("120713",
                    new[] { "yyyyMMdd", "yyMMdd" },
                    CultureInfo.InvariantCulture,
                    DateTimeStyles.None);

对于您的输出,您可以执行 start.ToString(dd.MM。 yyyy)

For your output you can do start.ToString("dd.MM.yyyy")

这篇关于C# - 如何验证DateTime(使“20120713”或“120713”TO“13.07.2012”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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