字符串转换为日期 [英] Convert String To Date
问题描述
我想转换 DD / MM / YYYY
到 MM / DD / YYYY
。
我的code:
字符串G =20-1-1999
DateTime的DT = DateTime.ParseExact(克,DD / MM / YYYY,CultureInfo.InvariantCulture);
错误消息:
字符串未被识别为有效的DateTime。
块引用>解决方案您必须给单个
M
的月份格式字符串。你可以阅读更多关于格式说明MM,这里和M < A HREF =http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx#M_Specifier相对=nofollow>此处。您还需要使用-
作为分隔符,而不是/
DateTime.ParseExact(GDD-M-yyyy,将CultureInfo.InvariantCulture);
如果您希望一天一个数字,你会给一个
D
的一天了。DateTime.ParseExact(G,D-M-yyyy,将CultureInfo.InvariantCulture);
的MM自定义格式说明
在MM自定义格式说明符重新presents月份从一个数字
01至12(或从1到13的日历有13
个月)。单月数字格式化带前导零, MSDN
块引用>的M自定义格式说明
在M自定义格式说明符重新presents月份从一个数字
1至12(或从1至13对具有13个月日历)。
单个数字一个月不带前导零的格式, MSDN
块引用>I want to convert
dd/MM/yyyy
toMM/dd/yyyy
.My code:
string g = "20-1-1999"; DateTime dt = DateTime.ParseExact(g, "dd/MM/yyyy", CultureInfo.InvariantCulture);
Error Message:
String was not recognized as a valid DateTime.
解决方案You have to give single
M
for month in format string. You can read more about format specifier MM, here and M here. You also need to using-
as separator instead of/
DateTime.ParseExact(g, "dd-M-yyyy", CultureInfo.InvariantCulture);
If you expect day single digit you would give single
d
for day too.DateTime.ParseExact(g, "d-M-yyyy", CultureInfo.InvariantCulture);
The "MM" Custom Format Specifier
The "MM" custom format specifier represents the month as a number from 01 through 12 (or from 1 through 13 for calendars that have 13 months). A single-digit month is formatted with a leading zero, MSDN
The "M" Custom Format Specifier
The "M" custom format specifier represents the month as a number from 1 through 12 (or from 1 through 13 for calendars that have 13 months). A single-digit month is formatted without a leading zero, MSDN.
这篇关于字符串转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!