Zed_Blade
Zed_Blade

Reputation: 1081

.NET DateTime Parse throwing exception

I've been having some issues which seem really really strange to me and I cannot event begin to comprehend why this is happening.

Basically, I'm trying to do a DateTime.ParseExact which if working on one case but not on the other.

Let's say I have this string as the date string:

"11/02/2015 11:59:06:313"

If I parse by giving the method the explicit declaration of the string, i.e. the next code, everything works fine:

DateTime.ParseExact("11/02/2015 11:59:06:313", "dd/MM/yyyy HH:mm:ss:fff", null);

Now, when placing this as a dynamic value (which is what I want) I get the "String not recognized as a valid DateTime format", in this code:

DateTime.ParseExact(item.original, "dd/MM/yyyy HH:mm:ss:fff", null);

I've also tried the Convert.ToDateTime method (which threw the same exception):

Convert.ToDateTime(item.original).ToString("dd/MM/yyyy HH:mm:ss:fff");

The 'item.original' variable is coming from a Class (it's a string property of that class, which is defined as

public class XmlData
{
    public string tableName { get; set; }
    public string columnName { get; set; }
    public string original { get; set; }
    public int required { get; set; }
    public string status { get; set; }
    public string type { get; set; }
    public string newValue { get; set; }
}

I'm really lost here. Does it make sense to anyone why this is happening?

EDIT

Decided to give a little more information on how this is being used because maybe the problem comes from further behind.

I have a class that I'm using to only define properties that, using reflection will create a DataTable.

The class has this property:

public DateTime last_date { get; set; }

Then I'm using this method to build the DataTable:

public DataTable CreateEmptyDataTable(Type myType)
{
    DataTable dt = new DataTable();

    foreach (PropertyInfo info in myType.GetProperties())
    {
        dt.Columns.Add(new DataColumn(info.Name, info.PropertyType));
    }

    return dt;
}

After initializing the DataTable, I'm reading the values from my XmlData class and using the following code to assing the value to the last_date column like this:

//Set the original state
foreach (XmlData item in collection)
{
    if (item.tableName == "vehicle")
    {
        if (item.original == "--NULL--")
            dr[item.columnName.Substring(item.tableName.Length + 1)] = DBNull.Value;
        else
        {
            if (item.type == "DT") //DateTime in format "dd/MM/yyyy HH:mm:ss:fff"
                dr[item.columnName.Substring(item.tableName.Length + 1)] = DateTime.ParseExact(item.original, "dd/MM/yyyy HH:mm:ss:FFF", null);
            else
                dr[item.columnName.Substring(item.tableName.Length + 1)] = Convert.ChangeType(item.original, dt.Columns[item.columnName.Substring(item.tableName.Length + 1)].DataType);
        }
    }
}

Upvotes: 1

Views: 1660

Answers (3)

Zed_Blade
Zed_Blade

Reputation: 1081

In case anyone is wondering, the "solution" for this problem was to instantiate a DateTime object by deconstructing the string and assigning it on the DateTime constructor like this:

string[] date = item.original.Split(' ');
string[] datePart = date[0].Split('/');
string[] hourPart = date[1].Split(':');

DateTime newDateValue = DateTime.MinValue;

if (hourPart.Length == 3)
{
    newDateValue = new DateTime(Convert.ToInt32(datePart[2]), Convert.ToInt32(datePart[1]), Convert.ToInt32(datePart[0]), Convert.ToInt32(hourPart[0]), Convert.ToInt32(hourPart[1]), Convert.ToInt32(hourPart[2]));
}
if (hourPart.Length == 4)
{
    newDateValue = new DateTime(Convert.ToInt32(datePart[2]), Convert.ToInt32(datePart[1]), Convert.ToInt32(datePart[0]), Convert.ToInt32(hourPart[0]), Convert.ToInt32(hourPart[1]), Convert.ToInt32(hourPart[2]), Convert.ToInt32(hourPart[3]));
}

I say "solution" with quotes because I think that's a bad solution and there's probably a better way around it because this operation is relatively heavy but at least it works

Upvotes: 0

MReis
MReis

Reputation: 56

Just a shot in the dark:

You have a working hardcoded value

DateTime.ParseExact("11/02/2015 11:59:06:313", "dd/MM/yyyy HH:mm:ss:fff", null);

But when you assign the value to the last_date column:

if (item.type == "DT") //DateTime in format "dd/MM/yyyy HH:mm:ss:fff"
            dr[item.columnName.Substring(item.tableName.Length + 1)] = DateTime.ParseExact(item.original, "dd/MM/yyyy HH:mm:ss:FFF", null);
        else
            dr[item.columnName.Substring(item.tableName.Length + 1)] = Convert.ChangeType(item.original, dt.Columns[item.columnName.Substring(item.tableName.Length + 1)].DataType);

shouldn't that be

DateTime.ParseExact(item.original, "dd/MM/yyyy HH:mm:ss:fff", null);

instead of

DateTime.ParseExact(item.original, "dd/MM/yyyy HH:mm:ss:FFF", null);

?

Upvotes: 3

Soner Gönül
Soner Gönül

Reputation: 98740

Your format is correct.

Since you use null as an IFormatProvider, I strongly suspect your CurrentCulture has different DateSeparator than / or/and different TimeSeparator than : characters.

/ and : characters are special in custom date and time parsing. They means as: replace me with current culture date or time separator

In your profile, it says you are from Portuguese, and your current culture will be probably pt-PT. And this culture has - as a DateSeparator

Upvotes: 4

Related Questions