Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert.ToDateTime('Datestring') to required dd-MM-yyyy format of date

Tags:

c#

I have string of Date which can be in any format of date but I wanted to convert it to dd-MM-yyyy format.
I have tried every Convert.ToDatetime option which converts only to the System format. I want it to convert dd-MM-yyyy format.

Please reply. Thanks in Advance.

like image 332
RaviKPalanisamy Avatar asked Aug 22 '16 04:08

RaviKPalanisamy


People also ask

What is the format of convert ToDateTime?

ToDateTime(value) Console. WriteLine("'{0}' converts to {1}.", value, convertedDate) Catch e As FormatException Console. WriteLine("'{0}' is not in the proper format.", value) End Try End Sub End Module ' The example displays the following output: ' '' converts to 1/1/0001 12:00:00 AM.

How do I convert date strings to mm dd yyyy?

Parsing String to Date //Instantiating the SimpleDateFormat class SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); Parse/convert the required String to Date object using the parse() method by passing it as a parameter.

How do I change the date format from yyyy mm dd in C #?

string date = DateTime. ParseExact(SourceDate, "dd/MM/yyyy", CultureInfo. InvariantCulture). ToString("yyyy-MM-dd");


2 Answers

Try this

DateTime dateTime = new DateTime();
dateTime = Convert.ToDateTime(DateTime.ParseExact("YouDateString", "dd-MM-yyyy", CultureInfo.InvariantCulture));

This will return DateTime with your Format

like image 171
Shakir Ahamed Avatar answered Oct 04 '22 22:10

Shakir Ahamed


Despite there being many answers and this being a duplicate answer, here are some possible solutions:

string formatted = date.ToString("dd-MM-yyyy");

or

string formatted = date.ToString("dd MM yyyy");

This link might help you with more formats and options.

like image 22
Keyur PATEL Avatar answered Oct 04 '22 22:10

Keyur PATEL