C#错误 - System.FormatException:String未被识别为有效的DateTime

时间:2015-06-02 12:23:48

标签: c# string datetime sitecore formatexception

我一直想在我的网站上输出一个时间。在我的localhost中,它渲染/运行正常,但是当我将文件上传到另一个环境/服务器时,它会产生错误“字符串未被识别为有效的DateTime”。我认为这是因为我正在使用的机器(我的笔记本电脑)的日期时间/时区文化与其他服务器不同。我应该用我的代码改变什么来与服务器匹配?

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];

}

1 个答案:

答案 0 :(得分:1)

使用starttime = DateTime.Parse(tempendtime, CultureInfo.InvariantCulture)或您当地的文化而不是Convert