Date box forced formatting logic

蹲街弑〆低调 提交于 2019-12-24 09:44:54

问题


So I have a program I use that I have become very intrigued by how they force their formatting of a date in a field when input. For example, if I put in "10217", the system would automatically force the field to become 10/02/2017. However, if I put in 1117, then nothing happens as it could be 01/01/2017, 11/17/??, or some other of many combinations. Does anyone know how this forced formatting might be being achieved regarding the logic?

Additionally, you can put in a date in the same field formatted as 10.2.17 and it would reformat to 10/2/2017. As well, if you input 1.1.17, it would reformat to 1/1/2017. Lastly, you can do the same thing of putting in the slashes and it will reformat the respective date format. So if I put in 10/2/17, it would reformat to 10/2/2017. Same this with typing 1/1/17 will reformat to 1/1/2017.

I've looked at the following link, but am not seeing anything that could be used to do this kind of logic. (Granted I could just be blatantly missing it.)

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

As well, I have seen this example surrounding a regex, but I am not familiar with this process.

Validating a date format string in c#

I understand this is a lot, but this all revolves around the date forced formatting logic. I am really not sure what logic to use to achieve what I want or what logic to chain together to achieve what I am looking for. I greatly appreciate all of the input.


回答1:


I think to approach this problem it may be necessary to see what they are using to parse the input of the user. If they are using DateTime.Parse then it will throw an exception when the string being parsed is ambiguous.

Of course, a programmer could always create their own way of parsing the input in the field. Though, typically a programmer isn't that enthusiastic about dealing with the ambiguous cases when parsing information. So let's assume they are working with a DateTime.Parse method for simplicity sake.

I created the following program to allow you to see when the parser sees something as ambiguous. The output of the program is shown in this picture:

The code demonstrating 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
        }

    }

}

In order to convert the DateTime back to a string you can do something similar to:

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");
}

You can even make your own parser with this information in this fashion so you can acheive a result for a date entered that is 3 characters long:

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


    }

Similarly, if you want to get the date back to a slash (/) seperated format in the form of a string without the minutes and hours and such..

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;



来源:https://stackoverflow.com/questions/47762726/date-box-forced-formatting-logic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!