简体   繁体   English

如何查看日期格式c#

[英]How to check date format c#

I have ac# block of code here: 我这里有ac#代码块:

    string inputString = textBox1.Text;
    DateTime dt;
    try
    {
        if (DateTime.TryParseExact(inputString, "yyyyMMdd", null, DateTimeStyles.None, out dt) == true)
        {
            dt = dt.AddMonths(6);
            textBox2.Text = dt.ToString("yyyyMMdd");
        }
        else if (DateTime.TryParseExact(inputString, "yyyy.MM.dd", null, DateTimeStyles.None, out dt) == true)
        {
            dt = dt.AddMonths(6);
            textBox2.Text = dt.ToString("yyyyMMdd");
        }
    }   
    catch (Exception ex) { MessageBox.Show(ex.Message); }

Basically, what it does is, user input a string in textbox1, on button click, C# will check what date format is in the textbox, then add 6 months on the date and output it in the textbox2 to string format yyyyMMdd. 基本上,它的作用是,用户在textbox1中输入一个字符串,在按钮点击时,C#将检查文本框中的日期格式,然后在日期上添加6个月并将其在textbox2中输出为字符串格式yyyyMMdd。 As you see as of now, it accepts yyyyMMdd and yyyy.MM.dd and do the same process. 如您所见,它接受yyyyMMdd和yyyy.MM.dd并执行相同的过程。 But my problem is I still got a lot of date format left: 但我的问题是我还有很多日期格式:

dd-MM-yy 
yyyy/mm/dd 
yyyy-mm-dd 
yy/mm/dd 

I don't want to use OR in my IF statement. 我不想在我的IF语句中使用OR。 Is there a way like WHATEVER the format is, will be accepted and do the process. 有没有像格式那样的方式,将被接受并执行该过程。 Thanks a lot guys! 非常感谢!

One of the overloads of TryParseExact accepts as its second parameter an array of strings. TryParseExact的 一个 重载接受一个字符串数组作为它的第二个参数。 It won't tell you exactly which format was used, however. 但是,它不会告诉您确切使用的格式。 If you really do need that information, then yes, you're just going to have to run the one-format TryParseExact with each format you want, and see which one works. 如果你确实需要这些信息,那么是的,你只需要运行你想要的每种格式的单格式TryParseExact,看看哪种格式有效。

And, of course, you'll need to make sure that all the formats you allow are unambiguous as to which parts are day, month, and year, or else you might parse 01/02/03 as any of six possible dates. 当然,您需要确保您允许的所有格式都明确指出哪些部分是日,月和年,否则您可能会将01/02/03解析为六个可能日期中的任何一个。

DateTime.TryParseExact has an overload that accepts multiple formats to try parsing. DateTime.TryParseExact有一个重载,它接受多种格式来尝试解析。

DateTime date;
DateTime.TryParseExact("11-Nov-13", new[] { "yyyy-MM-dd","MM/dd/yy","dd-MMM-yy"}, null, DateTimeStyles.None, out date); 

However the comment from Lee Taylor is still correct in that if you have formats that can be ambiguous, you will not know exactly what's wrong. 然而,李泰勒的评论仍然是正确的,因为如果你的格式可能含糊不清,你就不会确切地知道什么是错的。

If your solution requires that you return a formatted string using the same style as provided - then you'll need to pass your formats into a loop and try individually. 如果您的解决方案要求您使用与提供的相同的样式返回格式化字符串 - 那么您需要将格式传递给循环并单独尝试。

You can check for all possible formats via using CultureInfo.CurrentUICulture.DateTimeFormat.GetAllDateTimePatterns() which is present under the namespace System.Globalization. 您可以使用CultureInfo.CurrentUICulture.DateTimeFormat.GetAllDateTimePatterns()检查所有可能的格式,该文件存在于命名空间System.Globalization下。

You can use DateTime.TryParseExact which has an overload that accepts multiple formats to try parsing - ParseExact(string s, string[] formats, IFormatProvider provider, DateTimeStyles style) 您可以使用DateTime.TryParseExact ,它具有接受多种格式的重载以尝试解析 - ParseExact(字符串s,字符串[]格式,IFormatProvider提供程序,DateTimeStyles样式)

Example Code : 示例代码:

private DateTime GetDate(string date)
{
    DateTime dateTime = new DateTime();
    string[] formats = CultureInfo.CurrentUICulture.DateTimeFormat.GetAllDateTimePatterns();

    try
    {
        dateTime = DateTime.ParseExact(date, formats, CultureInfo.InvariantCulture, DateTimeStyles.None);
    }
    catch
    {
        throw ...;
    }

    return dateTime;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM