C#错误-System.FormatException:字符串未被识别为有效的日期时间
我一直试图在我的网站上输出一段时间。在我的localhost中,它呈现/运行良好,但当我将文件上载到另一个环境/服务器时,它会产生错误“字符串未被识别为有效的日期时间”。我假设这是因为我使用的计算机(我的笔记本电脑)的日期时间/时区文化与其他服务器不同。我应该用我的代码修改什么以与服务器匹配C#错误-System.FormatException:字符串未被识别为有效的日期时间,c#,string,datetime,sitecore,formatexception,C#,String,Datetime,Sitecore,Formatexception,我一直试图在我的网站上输出一段时间。在我的localhost中,它呈现/运行良好,但当我将文件上载到另一个环境/服务器时,它会产生错误“字符串未被识别为有效的日期时间”。我假设这是因为我使用的计算机(我的笔记本电脑)的日期时间/时区文化与其他服务器不同。我应该用我的代码修改什么以与服务器匹配 DateTime starttime; DateTime endtime; starttime = Convert.ToDateTime(tempstarttime); tempstarttime = s
DateTime starttime;
DateTime endtime;
starttime = Convert.ToDateTime(tempstarttime);
tempstarttime = starttime.ToString();
endtime = Convert.ToDateTime(tempendtime);
tempendtime = starttime.ToString();
if (schedlist[i, 2] == "PM" && schedlist[i + 1, 2] == "AM")
{
//reformat schedlist[i+1, 2] to next day date + schedlist[i, 1]
endtime = endtime.AddDays(1);
exceedtonextday = 1;
}
if (exceedtonextday == 1)
{
endtime = endtime.AddDays(1);
starttime = starttime.AddDays(1);
}
if (comparetimesched(starttime, endtime))
{
currentshow = "<span>" + schedlist[i, 1] + " " + schedlist[i, 2] + "</span><p>" + schedlist[i, 0] + "</p>";
nextshow = "<span>" + schedlist[i + 1, 1] + " " + schedlist[i + 1, 2] + "</span><p>" + schedlist[i + 1, 0] + "</p>";
showingimage = schedlist[i, 4];
showingimagetwo = schedlist[i + 1, 4];
}
DateTime开始时间;
日期时间结束时间;
starttime=Convert.ToDateTime(tempstarttime);
tempstarttime=starttime.ToString();
endtime=Convert.ToDateTime(tempendtime);
tempendtime=starttime.ToString();
如果(时间表[i,2]=“PM”&&i+1,2]=“AM”)
{
//将时间表[i+1,2]重新格式化为次日日期+时间表[i,1]
endtime=endtime.AddDays(1);
exceedtonextday=1;
}
如果(exceedtonextday==1)
{
endtime=endtime.AddDays(1);
starttime=starttime.AddDays(1);
}
if(比较时间(开始时间、结束时间))
{
currentshow=“”+schedlist[i,1]+“+schedlist[i,2]+”“+schedlist[i,0]+””;
nextshow=“”+schedlist[i+1,1]+“+schedlist[i+1,2]+”“+schedlist[i+1,0]+””;
showingimage=schedlist[i,4];
showingimagetwo=schedlist[i+1,4];
}
使用starttime=DateTime.Parse(tempendtime,CultureInfo.InvariantCulture)
或您的本地文化,而不是Convert
什么是tempstarttime
和tempendtime
?什么是您的CurrentCulture
?还有,为什么:starttime=Convert.ToDateTime
(tempstarttime);tempstarttime=starttime.ToString();?因此,您需要将字符串转换为DateTime
,然后再转换回字符串。值从何而来,您真的需要将其作为字符串获取吗?(另外,我认为您应该检查.NET命名约定,作为旁白…)使用特定区域性独立于操作系统区域性。请参见此处: