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, 2022 in C# by jyoti
• 1,240 points
14,445 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, 2022 by krishna
• 2,820 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, 2022 in C# by krishna
• 2,820 points
587 views
0 votes
0 answers

Convert XML String to Object

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

Jun 11, 2022 in C# by krishna
• 2,820 points
1,340 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, 2022 in C# by rajiv
• 1,620 points
1,606 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, 2022 in C# by rajiv
• 1,620 points
801 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,250 points
2,864 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, 2022 in Other DevOps Questions by Kichu
• 19,040 points
1,322 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, 2022 in C# by jyoti
• 1,240 points
1,552 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, 2022 in C# by krishna
• 2,820 points
354 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, 2022 in C# by krishna
• 2,820 points
1,768 views
0 votes
1 answer

C# DateTime to "YYYYMMDDHHMMSS" format

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

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