Click here to Skip to main content
13,042,329 members (105,562 online)
Rate this:
Please Sign up or sign in to 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 10-Mar-11 7:22am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Here is the answer with the code sample:[^].

For more flexible option, see this: the class System.Globalization.DateTimeFormatInfo and the sample code provided:[^], in particular, pay attention for "SortableDateTimePattern" in the sample code and the class.

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

Solution 3

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

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

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 10 Mar 2011
Copyright © CodeProject, 1999-2017
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