Convert strings to datetime

Is it possible to convert the following date strings to DateTime objects?

 "Friday 31 August 2012"; "26-Jul-2012" "190811" 

I tried in this form:

 DateTime.TryParse("Friday 31 August 2012" , CultureInfo.InvariantCulture.NumberFormat , DateTimeStyles.None , out tradeDate); 

but that will not work.

+4
source share
4 answers

Is it possible to convert these lines at the right time? "Friday August 31, 2012"; "26-Jul-2012" "190811"

Yes, since you parse dates in specific formats, you should use DateTime.TryParseExact . DateTime.TryParse will internally call DateTime.Parse , which will try to parse dates only using some predefined formats. From the docs:

The DateTime.Parse (String) method attempts to convert a string representation of a date and time into its DateTime equivalent. The string to be parsed can take any of the following forms:

  • A string with a date and time component.

  • A string with a date but no time component.

  • A string with time, but not with a date component.

  • A string that includes time zone information that complies with ISO 8601. For example, the first of the following two lines indicates Coordinated Universal Time (UTC); the second indicates the time in the time zone seven hours earlier than UTC:

    2008-11-01T19:35:00.0000000Z

    2008-11-01T19:35:00.0000000-07:00

  • A string that includes the GMT symbol and corresponds to the temporary format RFC 1123. For example:

    Sat, 01 Nov 2008 19:35:00 GMT

  • A string that includes the date and time, as well as information about the time zone offset. For instance:

    03/01/2009 05:42:00 -5:00

It is important to note that when you do not provide these methods with any culture information obtained from the current stream, which, if you have not explicitly indicated it, tends to be like your car. Therefore, if the dates you are trying to analyze have a certain culture, they will be more likely than not to understand normally, for example. both "Friday 31 August 2012" / "26-Jul-2012" analyze using TryParse for me, since my default culture is en-GB, however "190811" fails (I would assume that for all cultures), as it is not a system recognized date format. Therefore, TryParseExact more reliable.

+3
source

You should use the ParseExact format if you know the DateTime format

So, to parse "Friday August 31, 2012," use

 DateTime tradeDate = DateTime.ParseExact("Friday 31 August 2012", "dddd dd MMMM yyyy", CultureInfo.InvariantCulture); 

The format also has an array overload, so you can specify a few instead of "dddd dd MMMM yyyy" for your parsing needs.

something like that

+2
source

You can use DateTime.TryParseExact with the appropriate formats:

 string[] dateStrings = new[] { "Friday 31 August 2012", "26-Jul-2012", "190811"}; DateTime date = DateTime.MinValue; string[] formats = new[] { "dddd dd MMMM yyyy", "dd-MMM-yyyy", "ddMMyy" }; IEnumerable<DateTime> dates = dateStrings .Where(ds => DateTime.TryParseExact(ds , formats , CultureInfo.InvariantCulture , DateTimeStyles.None , out date)) .Select(ds => date); 

Custom Date and Time Format Strings

DEMO with your sample data

+1
source

Use TryParseExact and supply an array of format strings:

 DateTime.TryParseExact("Friday 31 August 2012" , new string[] {"dddd dd MMMM yyyy", "dd-MMM-yyyy", "ddMMyy"} , CultureInfo.InvariantCulture.NumberFormat , DateTimeStyles.None , out tradeDate); 

I suggest reading Custom Date and Time Strings in MSDN.

0
source

All Articles