我想以DateTime格式转换字符串
CultureInfo cultureInfoProvider = new CultureInfo("en-US");
var lastUpdatedDate = updatedVAlue.Max(); //value is 20150219T045452:635599184921416348
DateTime dt = DateTime.ParseExact(lastUpdatedDate,"yyyyMMddTHHmmss:fff",cultureInfoProvider);
我得到了一个例外 字符串未被识别为有效的DateTime。
答案 0 :(得分:1)
CultureInfo cultureInfoProvider = new CultureInfo("en-US");
var lastUpdatedDate = updatedVAlue.Max();
//value is 20150219A045452.635-5
DateTime dt = DateTime.ParseExact("20150219A045452.635-5", "yyyyMMddtHHmmss.fffz", cultureInfoProvider);
答案 1 :(得分:1)
你在这里。 这将修剪你的弦并保持它的3位尾。
CultureInfo cultureInfoProvider = new CultureInfo("en-US");
string lastUpdatedDate = updatedVAlue.Max(); //value is 20150219T045452:635599184921416348
string[] parse = lastUpdatedDate.Split(':');
lastUpdatedDate = parse[0] + ":" + parse[1].Substring(0,3);
DateTime dt = DateTime.ParseExact(lastUpdatedDate,"yyyyMMddTHHmmss:fff",cultureInfoProvider);
在这里测试:https://ideone.com/EIs600
注意:如果可能,请尽量避免在C#中使用var
,在这种情况下,string
将完成此任务。
答案 2 :(得分:0)
635599184921416348部件有问题。它是 太长,只支持最多七个digites。试试这个..
CultureInfo cultureInfoProvider = new CultureInfo("en-US");
var lastUpdatedDate = "20150219T045452:6355991";
DateTime dt = DateTime.ParseExact(lastUpdatedDate, "yyyyMMddTHHmmss:fffffff", cultureInfoProvider);