下面的示例说明 Parse 方法。[Visual Basic] Dim strMyDateTime As String strMyDateTime = "2/16/1992 12:15:12"' myDateTime gets Feburary 16, 1992, 12 hours, 15 min and 12 sec. Dim myDateTime As System.DateTime myDateTime = System.DateTime.Parse(strMyDateTime)Dim format As New System.Globalization.CultureInfo("fr-FR", True)' Reverse month and day to conform to a different format. Dim strMyDateTimeFrench As String strMyDateTimeFrench = " 16/02/1992 12:15:12"' myDateTimeFrench gets Feburary 16, 1992, 12 hours, ' 15 min and 12 sec. Dim myDateTimeFrench As System.DateTime myDateTimeFrench = System.DateTime.Parse( _ strMyDateTimeFrench, _ format, _ System.Globalization. _ DateTimeStyles.NoCurrentDateDefault)Dim expectedFormats As String() = {"G", "g", "f", "F"} ' myDateTimeFrench gets Feburary 16, 1992, 12 hours, ' 15 min and 12 sec. myDateTimeFrench = System.DateTime.ParseExact( _ strMyDateTimeFrench, _ expectedFormats, _ format, _ System.Globalization. _ DateTimeStyles.AllowWhiteSpaces)[C#] string strMyDateTime = "2/16/1992 12:15:12";// myDateTime gets Feburary 16, 1992, 12 hours, 15 min and 12 sec. System.DateTime myDateTime = System.DateTime.Parse(strMyDateTime);System.IFormatProvider format = new System.Globalization.CultureInfo("fr-FR", true);// Reverse month and day to conform to a different format. string strMyDateTimeFrench = " 16/02/1992 12:15:12";// myDateTimeFrench gets Feburary 16, 1992, 12 hours, // 15 min and 12 sec. System.DateTime myDateTimeFrench = System.DateTime.Parse(strMyDateTimeFrench, format, System.Globalization. DateTimeStyles.NoCurrentDateDefault);
string[] expectedFormats = {"G", "g", "f" ,"F"}; // myDateTimeFrench gets Feburary 16, 1992, 12 hours, // 15 min and 12 sec. myDateTimeFrench = System.DateTime.ParseExact(strMyDateTimeFrench, expectedFormats, format, System.Globalization. DateTimeStyles.AllowWhiteSpaces);[C++] String* strMyDateTime = S"2/16/1992 12:15:12";// myDateTime gets Feburary 16, 1992, 12 hours, 15 min and 12 sec. System::DateTime myDateTime = System::DateTime::Parse(strMyDateTime);System::IFormatProvider* format = new System::Globalization::CultureInfo(S"fr-FR", true);// Reverse month and day to conform to a different format. String* strMyDateTimeFrench = S" 16/02/1992 12:15:12";// myDateTimeFrench gets Feburary 16, 1992, 12 hours, // 15 min and 12 sec. System::DateTime myDateTimeFrench = System::DateTime::Parse(strMyDateTimeFrench, format, System::Globalization:: DateTimeStyles::NoCurrentDateDefault);String* expectedFormats[] = {S"G", S"g", S"f" , S"F"}; // myDateTimeFrench gets Feburary 16, 1992, 12 hours, // 15 min and 12 sec. myDateTimeFrench = System::DateTime::ParseExact(strMyDateTimeFrench, expectedFormats, format, System::Globalization:: DateTimeStyles::AllowWhiteSpaces);
Dim strMyDateTime As String
strMyDateTime = "2/16/1992 12:15:12"' myDateTime gets Feburary 16, 1992, 12 hours, 15 min and 12 sec.
Dim myDateTime As System.DateTime
myDateTime = System.DateTime.Parse(strMyDateTime)Dim format As New System.Globalization.CultureInfo("fr-FR", True)' Reverse month and day to conform to a different format.
Dim strMyDateTimeFrench As String
strMyDateTimeFrench = " 16/02/1992 12:15:12"' myDateTimeFrench gets Feburary 16, 1992, 12 hours,
' 15 min and 12 sec.
Dim myDateTimeFrench As System.DateTime
myDateTimeFrench = System.DateTime.Parse( _
strMyDateTimeFrench, _
format, _
System.Globalization. _
DateTimeStyles.NoCurrentDateDefault)Dim expectedFormats As String() = {"G", "g", "f", "F"}
' myDateTimeFrench gets Feburary 16, 1992, 12 hours,
' 15 min and 12 sec.
myDateTimeFrench = System.DateTime.ParseExact( _
strMyDateTimeFrench, _
expectedFormats, _
format, _
System.Globalization. _
DateTimeStyles.AllowWhiteSpaces)[C#]
string strMyDateTime = "2/16/1992 12:15:12";// myDateTime gets Feburary 16, 1992, 12 hours, 15 min and 12 sec.
System.DateTime myDateTime =
System.DateTime.Parse(strMyDateTime);System.IFormatProvider format =
new System.Globalization.CultureInfo("fr-FR", true);// Reverse month and day to conform to a different format.
string strMyDateTimeFrench = " 16/02/1992 12:15:12";// myDateTimeFrench gets Feburary 16, 1992, 12 hours,
// 15 min and 12 sec.
System.DateTime myDateTimeFrench =
System.DateTime.Parse(strMyDateTimeFrench,
format,
System.Globalization.
DateTimeStyles.NoCurrentDateDefault);
string[] expectedFormats = {"G", "g", "f" ,"F"};
// myDateTimeFrench gets Feburary 16, 1992, 12 hours,
// 15 min and 12 sec.
myDateTimeFrench =
System.DateTime.ParseExact(strMyDateTimeFrench,
expectedFormats,
format,
System.Globalization.
DateTimeStyles.AllowWhiteSpaces);[C++]
String* strMyDateTime = S"2/16/1992 12:15:12";// myDateTime gets Feburary 16, 1992, 12 hours, 15 min and 12 sec.
System::DateTime myDateTime =
System::DateTime::Parse(strMyDateTime);System::IFormatProvider* format = new System::Globalization::CultureInfo(S"fr-FR", true);// Reverse month and day to conform to a different format.
String* strMyDateTimeFrench = S" 16/02/1992 12:15:12";// myDateTimeFrench gets Feburary 16, 1992, 12 hours,
// 15 min and 12 sec.
System::DateTime myDateTimeFrench =
System::DateTime::Parse(strMyDateTimeFrench,
format,
System::Globalization::
DateTimeStyles::NoCurrentDateDefault);String* expectedFormats[] = {S"G", S"g", S"f" , S"F"};
// myDateTimeFrench gets Feburary 16, 1992, 12 hours,
// 15 min and 12 sec.
myDateTimeFrench =
System::DateTime::ParseExact(strMyDateTimeFrench,
expectedFormats,
format,
System::Globalization::
DateTimeStyles::AllowWhiteSpaces);
里面定义了详细的格式
成员名称 说明 值
AdjustToUniversal
受 .NET Framework 精简版的支持。
指示日期和时间必须转换成统一时间或格林尼治平均时间 (GMT)。 16
AllowInnerWhite
受 .NET Framework 精简版的支持。
指示在分析期间必须忽略字符串中的额外空白字符,那些空白字符出现在 DateTimeFormatInfo 格式模式中的情况除外。 4
AllowLeadingWhite
受 .NET Framework 精简版的支持。
指示在分析期间必须忽略前导空白字符,那些空白字符出现在 DateTimeFormatInfo 格式模式中的情况除外。 1
AllowTrailingWhite
受 .NET Framework 精简版的支持。
指示在分析期间必须忽略结尾空白字符,那些空白字符出现在 DateTimeFormatInfo 格式模式中的情况除外。 2
AllowWhiteSpaces
受 .NET Framework 精简版的支持。
指示在分析期间必须忽略字符串的任何位置上的额外空白字符,那些空白字符出现在 DateTimeFormatInfo 格式模式中的情况除外。该值是 AllowLeadingWhite、AllowTrailingWhite 和 AllowInnerWhite 值的组合。 7
NoCurrentDateDefault
受 .NET Framework 精简版的支持。
指示如果分析的字符串只包含时间而不包含日期,则 DateTime.Parse 和 DateTime.ParseExact 方法假定以下格林尼治日期:年 = 1、月 = 1 并且日 = 1。如果没有使用该值,则假定为当前日期。 8
None
受 .NET Framework 精简版的支持。
指示必须使用默认的格式设置选项。这是 DateTime.Parse 和 DateTime.ParseExact 的默认样
贴自.net Framework sdk文档
-------------------------
不过偶觉得一般不会有问题的.
d ShortDatePattern
D LongDatePattern
f 完整日期和时间(长日期和短时间)
F FullDateTimePattern(长日期和长时间)
g 常规(短日期和短时间)
G 常规(短日期和长时间)
m、M MonthDayPattern
r、R RFC1123Pattern
s 使用当地时间的 SortableDateTimePattern(基于 ISO 8601)
t ShortTimePattern
T LongTimePattern
u UniversalSortableDateTimePattern 用于显示通用时间的格式
U 使用通用时间的完整日期和时间(长日期和长时间)
y、Y YearMonthPattern 下表列出了可被合并以构造自定义模式的模式。这些模式是区分大小写的;例如,识别“MM”,但不识别“mm”。如果自定义模式包含空白字符或用单引号括起来的字符,则输出字符串页也将包含这些字符。未定义为格式模式的一部分或未定义为格式字符的字符按其原义复制。格式模式 说明
d 月中的某一天。一位数的日期没有前导零。
dd 月中的某一天。一位数的日期有一个前导零。
ddd 周中某天的缩写名称,在 AbbreviatedDayNames 中定义。
dddd 周中某天的完整名称,在 DayNames 中定义。
M 月份数字。一位数的月份没有前导零。
MM 月份数字。一位数的月份有一个前导零。
MMM 月份的缩写名称,在 AbbreviatedMonthNames 中定义。
MMMM 月份的完整名称,在 MonthNames 中定义。
y 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示不具有前导零的年份。
yy 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示具有前导零的年份。
yyyy 包括纪元的四位数的年份。
gg 时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。
h 12 小时制的小时。一位数的小时数没有前导零。
hh 12 小时制的小时。一位数的小时数有前导零。
H 24 小时制的小时。一位数的小时数没有前导零。
HH 24 小时制的小时。一位数的小时数有前导零。
m 分钟。一位数的分钟数没有前导零。
mm 分钟。一位数的分钟数有一个前导零。
s 秒。一位数的秒数没有前导零。
ss 秒。一位数的秒数有一个前导零。
f 秒的小数精度为一位。其余数字被截断。
ff 秒的小数精度为两位。其余数字被截断。
fff 秒的小数精度为三位。其余数字被截断。
ffff 秒的小数精度为四位。其余数字被截断。
fffff 秒的小数精度为五位。其余数字被截断。
ffffff 秒的小数精度为六位。其余数字被截断。
fffffff 秒的小数精度为七位。其余数字被截断。
t 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项的第一个字符(如果存在)。
tt 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项(如果存在)。
z 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数没有前导零。例如,太平洋标准时间是“-8”。
zz 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数有前导零。例如,太平洋标准时间是“-08”。
zzz 完整时区偏移量(“+”或“-”后面跟有小时和分钟)。一位数的小时数和分钟数有前导零。例如,太平洋标准时间是“-08:00”。
: 在 TimeSeparator 中定义的默认时间分隔符。
/ 在 DateSeparator 中定义的默认日期分隔符。
% c 其中 c 是格式模式(如果单独使用)。如果格式模式与原义字符或其他格式模式合并,则可以省略“%”字符。
\ c 其中 c 是任意字符。照原义显示字符。若要显示反斜杠字符,请使用“\\”。 只有上面第二个表中列出的格式模式才能用于创建自定义模式;在第一个表中列出的标准格式字符不能用于创建自定义模式。自定义模式的长度至少为两个字符;例如, DateTime.ToString( "d") 返回 DateTime 值;“d”是标准短日期模式。
DateTime.ToString( "%d") 返回月中的某天;“%d”是自定义模式。
DateTime.ToString( "d ") 返回后面跟有一个空白字符的月中的某天;“d”是自定义模式。
只能为固定区域性或特定区域性创建 DateTimeFormatInfo 或 NumberFormatInfo,而不能为非特定区域性创建它们。有关固定区域性、特定区域性和非特定区域性的更多信息,请参见 CultureInfo 类。该类实现 ICloneable 接口以启用 DateTimeFormatInfo 对象的复制。它还实现 IFormatProvider 以便为应用程序提供格式化信息。
-----------------------------------------
什么东东???to_date?????
CultureInfo ci = new CultureInfor("zh-CN);
string strDt = dt.ToString("d",ci);
DataFormatString="{0:yyyy-MM-dd hh:mm:ss}"显示为2005-7-31 03:03:54
DataFormatString="{0:yyyy-MM-dd HH:mm:ss}"显示为2005-7-31 15:03:54
HH:24小时
hh; 12小时
具体是否使用zh-CN还是其他文化可以在程序里设CultureInfo来设定,不设定的话会读机器上控制面板里区域语言环境设置那的长日期短日期时间格式来匹配。在zh-CN文化下,2005-7-31 15:03:54 是长时间格式(DateTime.ToString()),
2005-7-31是短日期格式(DateTime.ToShortDateString()),
2005年7月31日是长日期格式(DateTime.ToLongDateString()),
楼主想知道你机器上什么文化显示怎样的日期值可以通过类似下面输出来看:
Response.Write("DateTime.ToLongDateString is"+ DateTime.ToLongDateString());
Response.Write("DateTime.Year is"+ DateTime.Year());
之类的语句。
DateTime.ToString()后面的参数来设定格式,如DateTime.ToString("t")表示年月日的日期格式。