2

I have a WPF window which contains a textbox in which the user can enter a date in any format that he desires and i have to parse it in a datetime variable, simple enough? I tried various formats, some of them work with datatime.parse some don't, so what should i do about such strings?

some e.g. formats (year is not mandatory):

  • 21 Sept
  • 21st Sept
  • 21 September
  • 21st September
  • 21 Sep
  • 21/09
  • 21-09

whatever catches the users fancy as long as it is a proper readable date, can anyone help me with this?

Update : based on comments, i am modifying the input string format....what if user can enter in any way but always day followed by month followed by year(optional).....is that manageable ??

Billy
  • 67
  • 1
  • 7
  • 2
    11/12 -> is it 11th Dec or 12th Nov? – Azodious Sep 21 '12 at 10:20
  • Look at http://msdn.microsoft.com/en-us/library/system.datetime.parseexact.aspx – ziyasal Sep 21 '12 at 10:20
  • 5
    And what if they enter `04/03`? Is that in March or April? Based on your `21/09` and `09/21` examples, *either* could be correct. Computers aren't good at guessing games. Give the user a Date picker of some kind, and make sure you get something the user *intended*. – Damien_The_Unbeliever Sep 21 '12 at 10:21
  • As long as you need to support 'any format' you can't do it as your code would have to read minds. If you can narrow it down to a set of supported formats, then it can be done. You understand the 'proper readable date' is quite a subjective thing, right? Also, what about users from other countries? – Zdeslav Vojkovic Sep 21 '12 at 10:22
  • you should ideally have a datetime picker :) – Vignesh.N Sep 21 '12 at 10:22
  • it is just a textbox and the design can only support that, it is supposed to be like a quick add...so cant use datepicker but i see your point but what if i only restrict format to like dd-mm-yyyy where mm is month in words and year is optional – Billy Sep 21 '12 at 10:26
  • @ZiyaSAL : tried the link but it does not support when days become 21st or 22nd – Billy Sep 21 '12 at 10:28
  • i have updated my question.....can it work out now ?? – Billy Sep 21 '12 at 10:49
  • @Billy - Instead of trying to support any date format, you should only support valid date formats, don't try to solve an unsolveable problem. What is with all the comment level answers, BrunoLM is literally the only person, to provide an actual answer. – Security Hound Sep 21 '12 at 12:44
  • just so you know, if anone is looking for a similar problem, i found the solution here : http://www.codeproject.com/Articles/33298/C-Date-Time-Parser.....had to modify the queries a bit....but it did work out... – Billy Sep 24 '12 at 08:06

4 Answers4

3

Its not an answer but an Idea:

Why dont you use a Datepicker wo ou spare yourself alot of Job trying to convert every possible date to a valid one, and it looks nice too. Take alook at WPF Toolkit's Calender Datepicker Control

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
2

Its futile to do what you are doing.

A user can insert n number ways of adding date. How will you judge what format he has inserted.

Look for calendar controls because calendar is same everywhere and any user no matter where he is will insert correct date in it. In that case you can ask calendar for selecteddate or something kind of property.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
2

There is no good answer to this problem, as best as I can tell.

DateTime.Parse still relies on the date being in a particular format. If you wanted - you could try multiple CultureInfos and hope that one of them works - but that would only work for a tiny subset of written dates real people would find acceptable.

Even if you wrote a lot of code to try and figure out all the possible ways regular people write dates - you'd still have globalization problems - some dates would be valid for interpretation in more than one way.

04/05 <-- What day is that?

Unless this is just for the sake of doing it; I think the best thing to do is simply provide a date-picker control or something similar that makes it easy for the user and gets you the date . Short of that - indicate the date format you expect.

Rob P.
  • 14,921
  • 14
  • 73
  • 109
2

DateTime.Parse works with the current CultureInfo unless you specify another info. Meaning:

21 Sept           // invalid
21st Sept         // invalid
21 September      // acceptable by `en` CultureInfo
21st September    // invalid
21 Sep            // acceptable by `en` CultureInfo
21/09             // invalid by `en` CultureInfo (09/21 is valid)
21-09             // invalid by `en` CultureInfo (09-21 is valid)

But if you want a nice way for users to input a data consider using a control that will always catch a valid value instead of relying that a user will input the exact information you need manually.

BrunoLM
  • 97,872
  • 84
  • 296
  • 452
  • If i could....i definitely would go for the control but the design is such that a datepicker cannot be accomodated....i have tried with datetime.parse but it does not work....will try with invariant cultureinfo though..... – Billy Sep 21 '12 at 11:03