Set the year that is assumed when parsing partial date values in .NET

Viewed 2330

I need to parse a date string which could be in any reasonable format. For example:

  • 2012-12-25
  • 25 december 2012
  • 25 dec
  • 17:35

Some of these strings contain ambiguous dates which can result in several possible DateTime values (E.g. 25 dec can be interpreted as 2012-12-25, 2011-12-25, 1066-12-25, etc).

The way DateTime.Parse currently handles these ambiguous values is by using the current system date to determine the context. So if the current date is 26th July 2012 the string 25 dec is assumed to be in the current year and is parsed as 2012-12-25

Is it somehow possible to change this behaviour and set the current date context myself?

4 Answers
Related