Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: DateTime VB.NET format
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 10-Mar-11 7:22am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v3
Comments
Marcus Kramer at 10-Mar-11 14:38pm
   
+5. Excellent.
SAKryukov at 10-Mar-11 14:51pm
   
Thank you Marcus; that wasn't hard :-)
--SA
William Winner at 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".
SAKryukov at 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 at 10-Mar-11 15:30pm
   
Good pointer
SAKryukov at 10-Mar-11 15:31pm
   
Even better now, thanks to William's note.
--SA
Dalek Dave at 10-Mar-11 16:34pm
   
Excellent answer
SAKryukov at 10-Mar-11 16:36pm
   
Thank you, Dalek,
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
v2

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



Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 10 Mar 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100