日期框强制格式化逻辑

时间:2017-12-11 22:50:24

标签: c# winforms date-formatting

所以我有一个我使用的程序,我对他们在输入时如何强制在字段中格式化日期感到非常好奇。例如,如果我输入" 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。

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

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

同样,我已经看到围绕正则表达式的这个例子,但我不熟悉这个过程。

Validating a date format string in c#

我理解这很多,但这一切都围绕着强制格式逻辑的日期。我真的不确定用什么逻辑来实现我想要的东西或者用什么逻辑连接在一起以实现我正在寻找的东西。我非常感谢所有的投入。

1 个答案:

答案 0 :(得分:1)

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

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

我创建了以下程序,以便您查看解析器何时看到不明确的内容。该程序的输出如下图所示:

enter image description here

演示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";
        }


    }

enter image description here

同样,如果你想以字符串的形式将日期恢复为斜杠(/)分隔格式而没有分钟和小时等等。

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;

enter image description here