Ric*_*ard 12 c# datetime parsing
如果我有一个格式的字符串,yyyyMMddHHmmssfff
例如20110815174346225
.如何从该String创建DateTime对象.我尝试了以下内容
DateTime TimeStamp = DateTime.Parse(Data[1], "yyyyMMddHHmmssfff");
Run Code Online (Sandbox Code Playgroud)
但是我收到这些错误:
Error 1 The best overloaded method match for 'System.DateTime.Parse(string, System.IFormatProvider)' has some invalid arguments C:\Documents and Settings\rkelly1\Desktop\sd\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 67 29 WindowsFormsApplication1
Error 2 Argument 2: cannot convert from 'string' to 'System.IFormatProvider' C:\Documents and Settings\rkelly1\Desktop\sd\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 67 53 WindowsFormsApplication1
Run Code Online (Sandbox Code Playgroud)
Jua*_*ala 18
var sDate = "20110815174346225";
var oDate = DateTime.ParseExact(sDate, "yyyyMMddHHmmssfff", CultureInfo.CurrentCulture);
Run Code Online (Sandbox Code Playgroud)
你必须使用
DateTime time = DateTime.ParseExact(String,String, IFormatProvider);
Run Code Online (Sandbox Code Playgroud)
第一个参数字符串将是您的日期。第二个参数字符串将是您的格式第三个参数是您的文化信息(即 IFormatProvider
所以你会有
DateTime TimeStamp = DateTime.ParseExact(Data[1],"yyyyMMddHHmmssfff",CultureInfo.InvariantCulture);
Run Code Online (Sandbox Code Playgroud)
区域性信息是“代表用于解释 s 的区域性的 CultureInfo 对象。其 DateTimeFormat 属性返回的 DateTimeFormatInfo 对象定义 s 中的符号和格式。” 来自 MSDN。
这是了解更多信息的链接。 http://msdn.microsoft.com/en-us/library/kc8s65zs.aspx
归档时间: |
|
查看次数: |
24558 次 |
最近记录: |