简体   繁体   English

无法将字符串识别为DateTime.ParseExact的有效参数

[英]String was not recognized as a valid parameter for DateTime.ParseExact

I'm using this code: 我正在使用此代码:

var sec = "163516";
TimeSpan time = TimeSpan.FromSeconds(double.Parse(sec));
DateTime butikDatetime = DateTime.Today.Add(time);
string dateTime = butikDatetime.ToString("dd-MM-yy HH:mm:ss");
DateTime date = DateTime.ParseExact(dateTime, "dd-MM-yy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);

But receive this error for ParseExact: 但是收到ParseExact的此错误:

Additional information: String was not recognized as a valid DateTime. 附加信息:字符串未被识别为有效的DateTime。

Somebody have an idea what is wrong? 有人知道出什么事了吗?

You are converting the butikDateTime to dateTime object without culture (framework will use the current thread culture) and later reconverting the using Invariant culture. 您将不带区域性的情况下将butikDateTime转换为dateTime对象(框架将使用当前线程的区域性),并稍后重新转换使用不变性的区域性。 Can you use Invariant culture while converting butikDateTime to string 您可以在将butikDateTime转换为字符串时使用不变文化吗

var sec = "163516";
TimeSpan time = TimeSpan.FromSeconds(double.Parse(sec));
DateTime butikDatetime = DateTime.Today.Add(time);
string dateTime = butikDatetime.ToString("dd-MM-yy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
DateTime date = DateTime.ParseExact(dateTime, "dd-MM-yy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);

Updated line 3 as : 将第3行更新为:

string dateTime = butikDatetime.ToString("dd-MM-yy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在DateTime.ParseExact上未将字符串识别为有效的DateTime - String was not recognized as a valid DateTime on DateTime.ParseExact DateTime.ParseExact字符串未被识别为有效的日期时间 - DateTime.ParseExact String not recognized as valid datetime DateTime.ParseExact:字符串未被识别为有效的DateTime - DateTime.ParseExact: String was not recognized as a valid DateTime DateTime.ParseExact()-无法将字符串识别为有效的DateTime - DateTime.ParseExact() - String was not recognized as a valid DateTime 使用DateTime.ParseExact时,字符串未被识别为有效的DateTime - String was not recognized as a valid DateTime when using DateTime.ParseExact Datetime.ParseExact“字符串未被识别为有效的日期时间”错误 - Datetime.ParseExact “String was not recognized as a valid DateTime” error DateTime.ParseExact赋予String未被识别为有效的DateTime。 - DateTime.ParseExact gives String was not recognized as a valid DateTime. DateTime.ParseExact FormatException字符串未被识别为有效的DateTime - DateTime.ParseExact FormatException String was not recognized as a valid DateTime DateTime.ParseExact给出错误:字符串未被识别为有效的DateTime - DateTime.ParseExact give the error: String was not recognized as a valid DateTime DateTime.ParseExact引发以下错误“字符串未被识别为有效的DateTime” - DateTime.ParseExact is raising the following error “String was not recognized as a valid DateTime”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM