-3

I've been trying to output a time in my website. In my localhost it renders/runs fine but when I uploaded my file to another environment/server it yields the error "String was not recognized as a valid DateTime." I assume it's because the culture of datetime/timezone of the machine(my laptop) I'm using and the other server are different. What should I alter with my codes to matched with the server?

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

}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
uhani_user
  • 1
  • 1
  • 5
  • 1
    What is an exemplary value of `tempstarttime` and `tempendtime`? – Tim Schmelter Jun 02 '15 at 12:24
  • And what is your `CurrentCulture`? – Soner Gönül Jun 02 '15 at 12:25
  • 3
    Also, why this: `starttime = Convert.ToDateTime(tempstarttime); tempstarttime = starttime.ToString();`? So your converting a string to `DateTime` and then back to string. – Tim Schmelter Jun 02 '15 at 12:26
  • Where are the values coming from, and do you *really* need to obtain them as strings at all? (Additionally, I think you should review .NET naming conventions, as an aside...) – Jon Skeet Jun 02 '15 at 12:27
  • Use a specific culture to be independant of the OS culture. See here: http://stackoverflow.com/questions/5590180/how-to-convert-a-datetime-string-to-a-current-culture-datetime-string – Eric Bole-Feysot Jun 02 '15 at 12:27

1 Answers1

1

Use starttime = DateTime.Parse(tempendtime, CultureInfo.InvariantCulture) or your local culture instead of Convert.

i486
  • 6,491
  • 4
  • 24
  • 41