Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello !!


I just want to format the returned date from the data reader into "yyyy/MM/dd"

I have a DataReader and TextBox, i want to set one of DataReader values in the TextBox ... This value represents a date value from SQL Server DB. I did the following:

txtDate.Text = Format(DR("ActionDate"),"yyyy/MM/dd")


and another time I did this:


Dim x As Date
x = DR("ActionDate")
txtDate.Text = x.ToString("yyyy/MM/dd")


no one of above methods working well .. although I searched a lot in MSDN and I tried more then 10 solutions but no in vain !!
Posted

Here is the answer with the code sample: http://msdn.microsoft.com/en-us/library/5hh873ya.aspx[^].

For more flexible option, see this: the class System.Globalization.DateTimeFormatInfo and the sample code provided: http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx[^], in particular, pay attention for "SortableDateTimePattern" in the sample code and the class.

—SA
 
Share this answer
 
v3
Comments
fjdiewornncalwe 10-Mar-11 14:38pm    
+5. Excellent.
Sergey Alexandrovich Kryukov 10-Mar-11 14:51pm    
Thank you Marcus; that wasn't hard :-)
--SA
William Winner 10-Mar-11 15:06pm    
Well, actually, to be precise with your wording, there is not exact sample at that link that shows how to format a string with "yyyy/MM/dd".
Sergey Alexandrovich Kryukov 10-Mar-11 15:31pm    
Well, thank you very much for helping me to be accurate. I removed "exact" and, more importantly, added more flexible way in next paragraph.
--SA
Yusuf 10-Mar-11 15:30pm    
Good pointer
if the formats here: Standard Date and Time Format Strings[^] aren't giving you the results you expect, then you may need to check your culture settings.
 
Share this answer
 
C#
1 .String.Format("{0:dd/MM/yyyy}", DateTime.Now)

26/09/2009

2. String.Format("{0:MM/dd/yyyy}", DateTime.Now)

 09/26/2009


3 . String.Format("{0:dd/MM/yy}", DateTime.Now)

 26/09/09


4. String.Format("{0:t}", DateTime.Now)

 12:22 PM


5.String.Format("{0:F}", DateTime.Now)


Saturday, September 26, 2009 12:22:25 PM
 
Share this answer
 
v2

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