Click here to Skip to main content
15,907,326 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
How you convert from a date to string in c#
Posted
Comments
Pheonyx 6-Feb-14 8:56am    
This sort of question proves you are not prepared to help yourself. I guarantee that if you put that exact question word for word into Google (or any decent / semi-decent search engine) you will get the answer to your question.
Peter Leow 6-Feb-14 9:30am    
There are abundant examples on the web, just ask google.
agent_kruger 6-Feb-14 12:02pm    
please search on google before asking here, sir.

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:
C#
myDate.ToString("d", new CultureInfo("en-US"));
 
Share this answer
 
Lots of ways to turn a DateTime into a string; try these:
C#
DateTime aDate = DateTime.Now;

string s1 = aDate.ToString();

string s2 = aDate.ToString("'Date:' yyyy-MM-dd 'Time:' HH:mm:ss");

string s3 = String.Format("{0}", aDate);
Set a break-point in your code, and inspect the values returned to get a sense of what is possible. There are other, advanced options, using IFormatProvider for dealing with DateTime to string conversion taking into account different languages; see: [^].
 
Share this answer
 
Comments
Karthik_Mahalingam 6-Feb-14 9:31am    
5
 
Share this answer
 
A date object (like most objects) has a .ToString() method on it.
Simply call that and hey presto your date is now a string.

If you Google ToString (I would but I think you should be able to do that yourself) you will get a list of overloads that are available to define formatting of the string etc.

Alternatively, you can do:

C#
String.Format({0}, mydateobject);
and that will convert it to a string.
 
Share this answer
 
Comments
BillWoodruff 6-Feb-14 9:22am    
Your code example will not compile because you omitted the required quotes around the zero.
Karthik_Mahalingam 6-Feb-14 9:31am    
yes
it's simple you can search on google. like this hit to link button http://lmgtfy.com/?q=how+convert+date+to+string+in+c%23+language[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900