日期框强制格式化逻辑

Date box forced formatting logic

所以我有一个我使用的程序,我对他们如何在输入时强制在字段中格式化日期感到非常感兴趣。比如我输入“10217”,系统会自动强制字段变成10/02/2017。但是,如果我输入 1117,则什么也不会发生,因为它可能是 01/01/2017、11/17/?? 或其他许多组合。有谁知道这种强制格式化在逻辑上是如何实现的?

此外,您可以在格式为 10.2.17 的同一字段中输入一个日期,它会重新格式化为 10/2/2017。同样,如果您输入 1.1.17,它将重新格式化为 1/1/2017。最后,你可以做同样的事情,把斜杠放进去,它会重新格式化相应的日期格式。因此,如果我输入 10/2/17,它将重新格式化为 10/2/2017。同样,输入 1/1/17 将重新格式化为 1/1/2017。

我查看了以下 link,但没有看到任何可用于执行此类逻辑的内容。 (当然我可能只是公然错过它。)

https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings

另外,我看过这个围绕正则表达式的例子,但我不熟悉这个过程。

我明白了很多,但这一切都围绕着日期强制格式化逻辑。我真的不确定要使用什么逻辑来实现我想要的或将什么逻辑链接在一起以实现我正在寻找的东西。我非常感谢所有的输入。

我认为要解决这个问题,可能有必要查看他们使用什么来解析用户的输入。如果他们使用 DateTime.Parse 那么当被解析的字符串不明确时它会抛出异常。

当然,程序员总是可以创建自己的方法来解析字段中的输入。但是,通常程序员并不热衷于在解析信息时处理模棱两可的情况。因此,为了简单起见,我们假设他们正在使用 DateTime.Parse 方法。

我创建了以下程序,让您可以看到解析器何时发现某些内容不明确。程序输出如下图所示:

代码演示DateTime.Parse:

static void Main(string[] args)
{
    string input = "";
    while(input != "exit" || input != "Exit")
    {
        Console.Write("Input: ");
        input = Console.ReadLine();

        string inputDate = input;
        //try to parse it
        try
        {
            DateTime date = DateTime.Parse(inputDate);
            Console.WriteLine(date+"\n");
        }
        catch
        {
            //Exceptions. String not valid because ambiguity
            Console.WriteLine("Ambiguous.\n");
            //In here can also perform other logic, of course
        }

    }

}

为了将 DateTime 转换回字符串,您可以执行类似以下操作:

try
{

    DateTime date = DateTime.Parse(inputDate);
    Console.WriteLine(date+"\n");
    string month = date.Month.ToString();
    string day = date.Day.ToString();
    string year = date.Year.ToString();
    string resultingString = month + " " + day + " " + year ;
    Console.WriteLine(resultingString);
}
catch(Exception e)
{
    //Exceptions. String not valid because ambiguity
    Console.WriteLine("Ambiguous");
}

您甚至可以以这种方式使用此信息创建您自己的解析器,这样您就可以获得输入的日期的 3 个字符长的结果:

    static void Main(string[] args)
    {
        string input = "";
        while(input != "exit" || input != "Exit")
        {
            Console.Write("Input: ");
            input = Console.ReadLine();

            string inputDate = input;

            try
            {

                DateTime date = DateTime.Parse(inputDate);
                Console.WriteLine(date+"\n");
                string month = date.Month.ToString();
                string day = date.Day.ToString();
                string year = date.Year.ToString();
                string resultingString = month + " " + day + " " + year;
                //string resultingString = month + "/" + day + "/" + year;
                Console.WriteLine(resultingString);
            }
            catch(Exception e)
            {
                //Exceptions. String not valid because ambiguity
                try
                {
                   Console.WriteLine( myParser(inputDate) );
                }
                catch
                {
                    Console.WriteLine("Ambiguous");
                }

                //Console.WriteLine("Ambiguous.\n");
                //In here can also perform other logic, of course
            }

        }

    }

    static string myParser(string input)
    {
        string month,day,year,date;

        switch (input.Length)
        {
            //In the case that it's 1 character in length 
            case 1:
                return "Ambiguous.";
            case 2:
                return "Ambiguous.";

            //did user mean #/#/200#?  hopefully not #/#/199#...
            case 3:
                month = input.Substring(0, 1);
                day = input.Substring(1, 1);
                year = input.Substring(2, 1);
                date = month + " " + day + " " + year;
                return date;
            //did user mean  # # 20## 
            //or             # ## 200# 
            //or             ## # 200#
            case 4:

                return "Ambiguous";
            //user probably means ## # ##
            case 5:
                return "Ambiguous";
            case 6:
                return "Ambiguous";
            default:
                return "Ambiguous";
        }


    }

同样,如果您想将日期恢复为斜杠 (/) 分隔格式的字符串形式,不带分钟和小时等..

case 3:
    month = input.Substring(0, 1);
    day = input.Substring(1, 1);
    year = input.Substring(2, 1);
    date = month + " " + day + " " + year;

    DateTime dateTimeObj = DateTime.Parse(date);

    month = dateTimeObj.Month.ToString();
    day = dateTimeObj.Day.ToString();
    year = dateTimeObj.Year.ToString();

    string resultingString = month + "/" + day + "/" + year;

    return resultingString;