As with most things that appear simple on the surface, there is a hidden complexity here. When you talk about converting a date to a string, it's important to realise a few things.
First of all, there is no inherent Date type in .NET. There is a
DateTime
object, and this indicates that dates always have a time component. So, if you are after the date part alone then you need to consider outputting the date using either
ToShortDateString()
or
ToLongDateString()
.
Outputting a date, by default, uses the
CurrentCulture
's
DateTimeFormatInfo
to control how the date is displayed. It's a very rare case that you should be interfering with this. User's tend to set this to the culture of their own country, so changing how it's output can end up causing confusion. Consider this example - the user wants to check on the 13th of November 2014 to see that they haven't missed an appointment and your application pops up to show that the appointment is on 11/12/2014 - the user thinks - great, I've got plenty of time. However, your application has overridden the format so it's using MM/dd/yyyy and the user thinks it's dd/MM/yyyy (that's what they set). The appointment date was actually on the 12th November.
If you need to change the format of the date, you can specify the format that it needs to be output using
.ToString(dateFormatSpecifier);
where the
dateFormatSpecifier
identifies how you want the date
to be output[
^]. Alternatively, you can use
ToString
with the
CultureInfo
that matches the output that you want. For instance, if you want to format the date as a US date, use:
myDate.ToString("d", new CultureInfo("en-US"));