shanish
shanish

Reputation: 1984

Datetime conversion in C# - using formats

I need to convert a String to DateTime format, for this I just tried like

DateTime.ParseExact(DateOfBirth,"MM/dd/yyyy",CultureInfo.InvariantCulture);

it's working fine when I pass the value like 05/30/2012.

But if I try to pass the value as 5/30/2012 its showing error:

String was not recognized as a valid DateTime

To fix this I tried like

DateTime.ParseExact(String.Format("{0:MM/dd/yyyy}", DateOfBirth), "MM/dd/yyyy",
                                                  CultureInfo.InvariantCulture);

it's still not working. Here If I try String.Format("{0:MM/dd/yyyy}", DateOfBirth) for the value 5/30/2012 its showing the same format instead of 05/30/2012.

How can I fix this, can anyone help me here...

check this link string to DateTime conversion in C#

Upvotes: 2

Views: 287

Answers (6)

Robin Maben
Robin Maben

Reputation: 23034

In case you need to make it culture-independent..

var dateTimeFormat = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentUICulture.Name).DateTimeFormat;
dateTimeFormat.ShortDatePattern = 
    Regex.Replace(Regex.Replace(dateTimeFormat.ShortDatePattern, "[M]+", "MM"), "[d]+", "dd");

var newDate = date.HasValue ? date.Value.DateTime.ToString("d", dateTimeFormat) : null;

Upvotes: 1

devuxer
devuxer

Reputation: 42344

Try just "d" instead of "MM/dd/yyyy".

So, the statement should be:

var date = DateTime.ParseExact(DateOfBirth, "d", CultureInfo.InvariantCulture);

The documentation for this is here: http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx

Edit

Oops, I misread the documentation. It should be "M/d/yyyy".

Upvotes: 1

Joachim Isaksson
Joachim Isaksson

Reputation: 180877

Since you have separators in your string (ie /), you can just do;

DateTime.ParseExact(DateOfBirth,"M/d/yyyy",CultureInfo.InvariantCulture);

That will parse either single or double digit days/months. When you use MM/dd/yyyy, you're requiring them both to be double digit numbers, and that's obviously not what you want in this case.

Upvotes: 1

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56697

Use M/d/yyyy instead of the format specifier you're using. Using only a single M matches months with leading zeros as well. This is also true for d.

Upvotes: 2

V4Vendetta
V4Vendetta

Reputation: 38200

There is an overload which might be of your interest

DateTime.ParseExact(DateOfBirth,
                    new[] { "MM/dd/yyyy", "M/dd/yyyy" },
                    CultureInfo.InvariantCulture,
                    DateTimeStyles.None);

This should help you take care of single as well as two digit month part (since 5 is failing for you as the format is MM)

Upvotes: 1

Alex
Alex

Reputation: 23300

assuming your DateOfBirth string is always separated by slashes, you could try something like this:

string[] dateParts = DateOfBirth.Split('/');
DateTime.ParseExact(string.Format("{0:00}", dateParts[0]) + "/" + string.Format("{0:00}", dateParts[1]) + "/" + string.Format("{0:0000}", dateParts[2]));

I think the issue is the format string can't be recognized since DateOfBirth is not a DateTime object. Thus, you enforce formatting by reformatting the string yourself

Upvotes: 1

Related Questions