Our Man in Bananas
Our Man in Bananas

Reputation: 5981

Date string cannot be parsed to valid DateTime

I have the below code in my app that reads datetimes from an xml file:

private void getConfig()
    {
    XDocument xdocument = XDocument.Load(@"C:\Salesforce\SFDIAuto\config.xml");
    IEnumerable<XElement> tasks = xdocument.Elements();
    foreach (var task in tasks)
        {
       IEnumerable<XElement> query = task.Elements("Triggers").Elements("CalendarTrigger");
       foreach (XElement result in query)
           {            
            CalendarTrigger[0] = (result.Element("Next").Value != "") ? Convert.ToDateTime(result.Element("Next").Value).ToString("MM/dd/yyyy HH:mm:ss") :
                    DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss");
            dScanDate.Value = Convert.ToDateTime(CalendarTrigger[0]);
            dScanTime.Value = Convert.ToDateTime(CalendarTrigger[0]);

            CalendarTrigger[1] = (result.Element("Last").Value != "") ? Convert.ToDateTime(result.Element("Last").Value).ToString("dd/MM/yyyy HH:mm:ss") : "";
            CalendarTrigger[2] = result.Element("Result").Value;
            CalendarTrigger[3] = "Ready";
            if (Convert.ToDateTime(result.Element("Next").Value) < DateTime.Now)
                    CalendarTrigger[3] = "Missed/Overdue";

                CalendarTrigger[4] = result.Element("ScheduleByDay").Element("DaysInterval").Value;

the xml config file has the below format:

<?xml version="1.0" encoding="utf-8"?>
<config>
    <Triggers>
        <CalendarTrigger>
            <Start>2012-07-02T18:00:00</Start>
            <Next>2013-11-13T21:00:00</Next>
            <Last>2013-11-12T21:00:00</Last>
            <Result>Success</Result>
            <Enabled>true</Enabled>
            <ScheduleByDay>
                <DaysInterval>1</DaysInterval>
            </ScheduleByDay>
       </CalendarTrigger>
    </Triggers>

on my development PC, there are no errors, but on the live server this procedure always throws an error:

String was not recognized as a valid DateTime.

the value it is looking at is:

CalendarTrigger[0] = "11/13/2013 21:00:00"

Seeing as it works fine on my development pc, what should I do to find out what is different in the environment, and how could I recode this part to work anywhere?

Upvotes: 2

Views: 9412

Answers (2)

Anatolii Gabuza
Anatolii Gabuza

Reputation: 6260

Problem is in specific machine culture. Convert.ToDateTime will parse string using system defined culture. For one system 11/13/2013 will be a valid date but for another (Ukrainian for example, where month should go on a second place :) ) it is not and valid should be 13/11/2013.

When you're storing numeric/date/locale specific data in strings you should always take into consideration client culture. To make things clearer I suggest you yo use InvarianCulture for storing/converting culture specific data.

Sample of invariant date stored in string field:

var invariantStringDate = DateTime.Now.ToString(System.Globalization.CultureInfo.InvariantCulture.DateTim‌​‌​eFormat);

Upvotes: 4

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98740

From Convert.ToDateTime Method (String)

If value is not null, the return value is the result of invoking the DateTime.Parse method on value using the formatting information in a DateTimeFormatInfo object that is initialized for the current culture. The value argument must contain the representation of a date and time in one of the formats described in the DateTimeFormatInfo topic.

In your case, looks like this format doesn't described in your current culture. My money is for your culture takes 13 as a month :)

Instead of this, you can use DateTime.ParseExact with CultureInfo.InvariantCulture like;

string s = "11/13/2013 21:00:00";
DateTime dt = DateTime.ParseExact(s, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
Console.WriteLine(dt);

Output will be;

11/13/2013 9:00:00 PM

Here a demonstration.

Upvotes: 2

Related Questions