+ 1

C# date box forced formatting

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. So 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/xx, or some other of many combinations. Does anyone know how this forced formatting might be being achieved? Thanks in advance.

10th Dec 2017, 2:47 AM
Ryan Wakefield
Ryan Wakefield - avatar
5 ответов
0
You could check if year.Length() < 4 And do the same for day and month
10th Dec 2017, 12:15 PM
Dextozz
Dextozz - avatar
0
I guess I am unsure of what you are meaning by this. I can't necessarily check if year length is less than 4 seeing as the input is a handful of numbers, not one box for year, one for day, and one for month. Thanks.
10th Dec 2017, 3:14 PM
Ryan Wakefield
Ryan Wakefield - avatar
0
Oh sorry, then i guess you could say string input = "12102014" string day = input[0] + input[1]; string month = input[2] + input[3] string year = same thing but with 4,5,6,7 Then you envelop thay inside try catch where Exception is array out of scope (Incase user inputs less than 8numbers)
10th Dec 2017, 4:01 PM
Dextozz
Dextozz - avatar
0
That's the thing, they can enter less than 8 characters. So a user will enter what I placed in the description above of 10217, which is 5 characters. However, it will then reformat it to 10/02/2017. So yeah, I am not sure that logic will 100% work. It could with some variances, but I feel like there should be an easier way to do this. Thanks.
10th Dec 2017, 4:21 PM
Ryan Wakefield
Ryan Wakefield - avatar
0
After looking around for a while, I found a solution to this issue. By using the datetime.parse method, you can control a lot of the formatting, time zones, etc. etc. :)
12th Dec 2017, 11:49 PM
Ryan Wakefield
Ryan Wakefield - avatar