user1894462
user1894462

Reputation: 51

String conversion to datetime

Is it possible to convert the below 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 it doesn't work.

Upvotes: 4

Views: 436

Answers (4)

James
James

Reputation: 82136

Is possible to convert this strings in correct datetime? "Friday 31 August 2012"; "26-Jul-2012" "190811"

Yep, as your parsing the dates in specific formats you should use DateTime.TryParseExact. DateTime.TryParse will internally call DateTime.Parse which will only attempt to parse the dates using some pre-defined formats. From the docs:

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

  • A string with a date and a time component.

  • A string with a date but no time component.

  • A string with a time but no date component.

  • A string that includes time zone information and conforms to ISO 8601. For example, the first of the following two strings designates the Coordinated Universal Time (UTC); the second designates the time in a 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 designator and conforms to the RFC 1123 time format. For example:

    Sat, 01 Nov 2008 19:35:00 GMT

  • A string that includes the date and time along with time zone offset information. For example:

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

It's important to note that when you don't supply these methods any sort of culture information it's taken from the current thread, which if you haven't set explicitly, tends to be that of your machine. So if the dates you are attempting to parse are of that particular culture they will more often than not parse ok e.g. both "Friday 31 August 2012"/"26-Jul-2012" parse using TryParse for me as I my default culture is en-GB, however, "190811" fails (I would imagine for all cultures) as it's not a system recognised date format. Hence why TryParseExact is more reliable.

Upvotes: 3

Tim Schmelter
Tim Schmelter

Reputation: 460238

You can use DateTime.TryParseExact with the proper 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

Upvotes: 1

V4Vendetta
V4Vendetta

Reputation: 38230

You should be using the ParseExact format when you know the format of DateTime

So to parse "Friday 31 August 2012" use

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

The format also has a array overload so you can specify multiple in place of "dddd dd MMMM yyyy" for your parsing needs.

something similar

Upvotes: 2

Oded
Oded

Reputation: 499262

Use TryParseExact and supply an array of formatting 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 up on Custom Date and Time Format Strings on MSDN.

Upvotes: 0

Related Questions