如何将日期值格式化为yyyy/MM/dd?
问题描述:
你好!!
我只想将数据读取器返回的日期格式化为"yyyy/MM/dd"
我有一个DataReader和TextBox,我想在TextBox中设置DataReader值之一.此值表示SQL Server DB中的日期值.我做了以下事情:
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")
以上方法中没有一种能很好地工作..尽管我在MSDN中进行了大量搜索,尝试了10种以上的解决方案,但没有白费了!!
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 !!
答
这就是答案与代码示例: http://msdn.microsoft.com/en-us/library/5hh873ya.aspx [ ^ ].
有关更灵活的选项,请参见:类System.Globalization.DateTimeFormatInfo
和提供的示例代码: http ://msdn.microsoft.com/zh-CN/library/system.globalization.datetimeformatinfo.aspx [-SA
Here is the answer with the code sample: http://msdn.microsoft.com/en-us/library/5hh873ya.aspx[^].
For more flexible option, see this: the classSystem.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
(如果此处使用以下格式:标准日期和时间格式字符串 [ ^ ]不能给您期望的结果,那么您可能需要检查您的区域性设置.
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.
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