Click here to Skip to main content
15,921,841 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am displaying date in grid view. it shows in gridview like this '1-9-2010' i.e in mm-dd-yyyy.
i want to display it in text box.
but i want to display it like this '1/9/2010'. how do i do that?
Posted

You call ToString on the DateTime object and if none of the built in methods help you, then you provide your own format string. I'd guess it's MM/dd/yyyy, but I could be wrong.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 9-Feb-12 8:07am    
No you are correct, all that needed a little tweaking were the format letters (capital m for month d for day and y for year both uncapitalized).

Cheers!
Christian Graus 9-Feb-12 8:08am    
Cool - thanks for that !!
use this
C#
String.Format("{0:M/d/yyyy}", dt);
 
Share this answer
 
See this short example as a reference:

C#
DateTime nu = DateTime.Now;
String myFormattedDate = nu.ToString("MM/dd/yyyy");


This is the MSDN page for custom string formats:http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].

Regards,

Manfred
 
Share this answer
 
string date2;
string date1 ="1/9/2010";
string [] x1;

x1=date1.Split('/');

date2 = x1[1] + "/" + x1[0] + "/" + x1[2];

Response.Write(date2);  // 9/1/2010
 
Share this answer
 
v2
Just learn all of the methods System.DataTime.ToString and standard or custom format strings (please see the MSDN articles referenced below).

Pay attention for the code sample showing how to use CultureInfo as IFormatProvider: http://msdn.microsoft.com/en-us/library/ht77y576.aspx[^].

Please see:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^],
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].

Pay attention that some standard formats produce different results for different cultures; and the culture used is defined by the current culture of the calling thread. To change the culture of the thread, use System.Threading.Thread.CurrentCulture, please see:
http://msdn.microsoft.com/en-us/library/system.threading.thread.currentculture.aspx[^].

—SA
 
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