本文介绍了无法将字符串识别为DateTime.ParseExact的有效参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码:
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);
但是收到ParseExact错误:
But receive this error for ParseExact:
有人知道出什么问题了?
Somebody have an idea what is wrong?
推荐答案
您正在将 butikDateTime
转换为没有文化的dateTime对象(框架将使用当前的线程区域性),并在以后重新转换使用不变式区域性。您可以在将butikDateTime转换为字符串时使用不变文化吗?
You are converting the butikDateTime
to dateTime object without culture (framework will use the current thread culture) and later reconverting the using Invariant culture. Can you use Invariant culture while converting butikDateTime to string
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);
将第3行更新为:
string dateTime = butikDatetime.ToString("dd-MM-yy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
这篇关于无法将字符串识别为DateTime.ParseExact的有效参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!