Is it possible to force C # DateTime.TryParse to require day, month and year?

I use DateTime.TryParseto parse the date of birth, which is captured from Chatbot. Due to the colloquial nature of Chatbot, users can enter their DOBs in a variety of formats, such as "jun 5 1980" or "3/21/1974". In this sense, it is DateTime.TryParseperfect, because it can perfectly parse these and other formats.

However, if you provide an incomplete date, such as β€œ10/24” or β€œJune 1977”, it TryParsewill be successful, and the missing date component will be installed by default. For DOB, this is not ideal, since we do not want to accept an incomplete date. Is there a way to prevent this default and tell the parser only to successfully parse a date that defines all three days, a month, and a year?

+4
source share
4 answers

You can use TryParseExact with several formats. It may seem a little tedious to list each format you want, but that way you'll be frank about it.

MSDN Documentation

, . en-US, ( ) .

DateTime dt;
var str = "6/5/2007";

if (DateTime.TryParseExact(str, "MM/DD/YYYY", new CultureInfo("en-US"), DateTimeStyles.None, out dt)
    || DateTime.TryParseExact(str, "MM/DD/YY", new CultureInfo("en-US"), DateTimeStyles.None, out dt)) 
{

}

DateTime.TryParseExact. :

DateTime dt;
var str = "6/5/2007";

if (DateTime.TryParseExact(str, new[] { "MM/DD/YYYY", "MM/DD/YY" }, new CultureInfo("en-US"), DateTimeStyles.None, out dt))
{

}
+3

TryParse: . , , , .
:

string myDateOK = "01/12/17";
DateTime.TryParse(myDateOK, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime myDateAsDate1);
var isFullDate_myDateAsDate1 = false;

string myDateKO = "12/17";
DateTime.TryParse(myDateKO, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime myDateAsDate2);
var isFullDate_myDateAsDate2 = false;


if (myDateAsDate1.Date.GetDateTimeFormats(CultureInfo.CurrentCulture).Contains(myDateOK))
{
    isFullDate_myDateAsDate1 = true;
}

if (myDateAsDate2.Date.GetDateTimeFormats(CultureInfo.CurrentCulture).Contains(myDateKO))
{
    isFullDate_myDateAsDate2 = true;
}

// => isFullDate_myDateAsDate1 = true and isFullDate_myDateAsDate2 = false
0

TryParseExact() . , , TryParseExact() , .

public static class DateTimeExtensions
{
    public static bool StrictTryParse(this string s, out DateTime result)
    {
        result = DateTime.MinValue;

        // List all the formats you want here.
        // Could move this out of the method, or make it a parameter.
        List<string> formats = new List<string> { "MM/dd/yyyy", "MM/dd/yy" };

        foreach (string format in formats)
        {
            if (DateTime.TryParseExact(s, format, CultureInfo.CurrentCulture, DateTimeStyles.None, out result))
                return true;
        }

        return false;
    }
}

Thus, you can call the extension method in StrictTryParse()more or less the same way as TryParse(), but with the necessary functionality.

string dateStr = "08/23/2017";
DateTime dt;
if (dateStr.StrictTryParse(out dt))
    Console.WriteLine("Date: {0}.", dt.ToLongDateString());
0
source

Source: https://habr.com/ru/post/1684327/


All Articles