Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert any type of date to dd/mm/yyyy

Tags:

c#

winforms

I receive text from a *.csv file in any date format

For example: dd/mm/yy or dd/mm/yyyy or mm/dd/yyyy or 4 may 2010......

How I can convert to just a single type of format: dd/mm/yyyy ?

I'm working on C#, .NET 3.5, WinForms

Thanks in advance

like image 908
Gold Avatar asked Jan 23 '26 00:01

Gold


2 Answers

If you're receiving data in multiple formats and you can't identify them, you've got problems. What does "09/07/2010" mean? September 7th or July 9th? This is the first thing to get straight in your mind, and it has nothing to do with technology. You have two contradictory formats - how are you going to deal with them? Sample the file and pick whichever looks most likely? Treat each line separately, favouring one format over another? Ask the user?

Once you've parsed the data correctly, formatting it in the desired way is easy, as per John's answer. Note that you must use "MM" for the month, not "mm" which represents minutes. You should also specify which culture to use (affecting the date separators) assuming you don't just want to take the system default.

like image 82
Jon Skeet Avatar answered Jan 24 '26 14:01

Jon Skeet


DateTime.Parse("your data").ToString("dd/MM/yyyy");
like image 32
John Gietzen Avatar answered Jan 24 '26 14:01

John Gietzen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!