Converting dd mm yyyy formatted string to Datetime

0 votes

I'm fresh to C# and DotNet. I'd want to convert a text in the format mm/dd/yyyy to a DateTime object. I attempted to use the parse function as shown below, but it failed due to a runtime error.

Any thoughts on how I can convert it to datetime?

DateTime dt=DateTime.Parse("24/01/2013");
Jun 11 in C# by jyoti
• 1,240 points
31 views

1 answer to this question.

0 votes

Sounds to me like you need DateTime.ParseExact (https://docs.microsoft.com/en-us/dotnet/api/system.datetime.parseexact?redirectedfrom=MSDN&view=net-6.0#overloads)

It would work something like this

string strDate = "24/01/2013";
DateTime date = DateTime.ParseExact(strDate, "dd/MM/yyyy", null)

Here, null will use the current culture so you can tweak it according to your requirement to ensure the consistent output

DateTime date = DateTime.ParseExact(strDate, "dd/MM/yyyy", CultureInfo.InvariantCulture)
answered Jul 4 by krishna
• 2,540 points

Related Questions In C#

0 votes
0 answers

Best way to reverse a string

I recently had to develop a string ...READ MORE

Jun 11 in C# by krishna
• 2,540 points
11 views
0 votes
0 answers

Convert XML String to Object

I'm getting XML strings over a socket ...READ MORE

Jun 11 in C# by krishna
• 2,540 points
40 views
0 votes
1 answer

What is the best C# to VB.net converter

Telerik has a solid SharpDevelop-based converter that ...READ MORE

answered May 30 in C# by rajiv
• 1,620 points
69 views
0 votes
1 answer

Questions every good .NET developer should be able to answer?

If I were interviewing someone who knew ...READ MORE

answered May 30 in C# by rajiv
• 1,620 points
41 views
0 votes
1 answer

Validating String against USPS state abbreviations

This is how I'd do it: private static ...READ MORE

answered Nov 10, 2018 in Others by DataKing99
• 8,240 points
1,267 views
0 votes
0 answers

String was not recognized as a valid DateTime " format dd/MM/yyyy"

I want to convert my string formatted ...READ MORE

May 1 in Other DevOps Questions by Kichu
• 19,040 points
197 views
0 votes
1 answer

How to get substring from string containing newlines

If you want to maintain the newline ...READ MORE

answered Jun 14 in C# by jyoti
• 1,240 points
19 views
0 votes
0 answers

How can I split a string with a string delimiter

Suppose this is my string My name is ...READ MORE

Jun 11 in C# by krishna
• 2,540 points
15 views
0 votes
1 answer

How to initialize a list of strings (List<string>) with many string values

Simply remove the () at the end. List<string> ...READ MORE

answered Jun 13 in C# by krishna
• 2,540 points
21 views
0 votes
1 answer

C# DateTime to "YYYYMMDDHHMMSS" format

Use this. It is case sensitive by ...READ MORE

answered Jun 13 in C# by krishna
• 2,540 points
61 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP