1

I have date in string format:

APRIL, 03/2013

How to convert to date object in C#

Dawood Awan
  • 7,051
  • 10
  • 56
  • 119
  • 1
    [DateTime.ParseExact](http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx), you just have to figure out the exact format string (shouldn't be difficult given the [MSDN documentation](http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx)). – Heinzi Jun 20 '13 at 14:18
  • Oops, wrong link. Anyway, this is a duplicate question of many others. Show what you have tried, your code, what your code does and what you expect it to do. – CodeCaster Jun 20 '13 at 14:18
  • 2
    Marking to reopen as the linked question is for a different language. Can then be re-closed linked to a better question (eg: http://stackoverflow.com/questions/919244/converting-string-to-datetime-c-net?rq=1) – Jon Egerton Jun 20 '13 at 14:20
  • @JonEgerton It looks like five separate people will be needed to close it. It won't let me do it now. – woz Jun 20 '13 at 14:42
  • @JonEgerton: Even if the other question also answers the question how to convert a string to `DateTime` it's not the best ducplicate since it wouldn't help to convert this string: `APRIL, 03/2013` – Tim Schmelter Jun 20 '13 at 15:05
  • 1
    @TimSchmelter: True, although the answer with most votes (by CMS) does cover `ParseExact` - its not unreasonable that it might be figured out from there. – Jon Egerton Jun 20 '13 at 15:11
  • @JonEgerton: I don't think so. `ParseExact` doesn't help to find the way to the correct format string for this exotic date-format, the cultureinfo issue or date-separator issue(`/`). You need to study a lot of sites to find the answer. Or simply use the one below ;-) – Tim Schmelter Jun 20 '13 at 18:35

1 Answers1

6

Use DateTime.ParseExact:

DateTime dt = DateTime.ParseExact("APRIL, 03/2013", "MMMM, dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None);

I have used CultureInfo.InvariantCulture(similar to english but culture independent) to enforce the / separator in the format and to ensure that it works in any culture.

Note that / has a special meaning in format strings. It means: replace me with the current culture's date separator. See: the "/" Custom Format Specifier.

Custom Date and Time Format Strings

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939