Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#错误-System.FormatException:字符串未被识别为有效的日期时间_C#_String_Datetime_Sitecore_Formatexception - Fatal编程技术网

C#错误-System.FormatException:字符串未被识别为有效的日期时间

C#错误-System.FormatException:字符串未被识别为有效的日期时间,c#,string,datetime,sitecore,formatexception,C#,String,Datetime,Sitecore,Formatexception,我一直试图在我的网站上输出一段时间。在我的localhost中,它呈现/运行良好,但当我将文件上载到另一个环境/服务器时,它会产生错误“字符串未被识别为有效的日期时间”。我假设这是因为我使用的计算机(我的笔记本电脑)的日期时间/时区文化与其他服务器不同。我应该用我的代码修改什么以与服务器匹配 DateTime starttime; DateTime endtime; starttime = Convert.ToDateTime(tempstarttime); tempstarttime = s

我一直试图在我的网站上输出一段时间。在我的localhost中,它呈现/运行良好,但当我将文件上载到另一个环境/服务器时,它会产生错误“字符串未被识别为有效的日期时间”。我假设这是因为我使用的计算机(我的笔记本电脑)的日期时间/时区文化与其他服务器不同。我应该用我的代码修改什么以与服务器匹配

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命名约定,作为旁白…)使用特定区域性独立于操作系统区域性。请参见此处: