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.
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.
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.
string date = DateTime. ParseExact(SourceDate, "dd/MM/yyyy", CultureInfo. InvariantCulture). ToString("yyyy-MM-dd");
Try this
DateTime dateTime = new DateTime();
dateTime = Convert.ToDateTime(DateTime.ParseExact("YouDateString", "dd-MM-yyyy", CultureInfo.InvariantCulture));
This will return DateTime
with your Format
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With