3

I want know is there good way to detect Column DataType for Date field (NOT DateTime)?

This what currently I do:

switch (dt.Columns[col].DataType.FullName)
{  
    case "System.DateTime":  
        formatedVal = Formatter.GetDateTime(val);  
        break;

    // which is NOT possible, but something equivalent am looking for
    case "System.Date":  
        formatedVal = Formatter.GetDate(val);  
        break;

    default:
        formatedVal = val.ToString();
        break;
}
2
  • What value do you find you're getting for the FullName property for your Date column? Commented Apr 15, 2010 at 14:55
  • Sam: SQL returns Date (with 12:00:00) and don't want to assume that as Date instead DateTime. Commented Apr 15, 2010 at 14:59

5 Answers 5

7

Nope. There is no Date type, there is only DateTime. If this is coming from a SQL Server 2008 table that uses a date column, it will be mapped to DateTime in .NET.

If you need to get at this information then you'll have to do it at the SQL level; by the time it's loaded into a DataTable, it's too late.

I'm not positive about your requirements, but it looks like you might be able to get away with just checking for midnight and using a different format for that:

DateTime dt = (DateTime)val;
return (dt == dt.Date) ? Formatter.GetDate(dt) : Formatter.GetDateTime(dt);
Sign up to request clarification or add additional context in comments.

Comments

1

DateTime.ToString is flexible http://msdn.microsoft.com/library/zdtaw1bw.aspx

Comments

1

I have used a slightly different approach to solve the same issue

switch (dt.Columns[col].DataType.FullName)
{
    case "System.DateTime":
        formatedVal = ((DateTime)dataRow[col]).ToString("d");
        break;

    case "System.Date":
        formatedVal = ((DateTime)dataRow[col]).ToString();
        break;

    default:
        formatedVal = dataRow[col].ToString();
        break;
}

Comments

0

If you use an SqlDataReader, you can call GetSchemaTable which will give you access to the underlying SQL Server datatypes for each column - there's a full list of the schema info it returns in that linked MSDN reference. So that's one approach if you really need to get the underlying SQL Server schema.

SqlDataAdapter does have a FillSchema method, but I don't think that actually gives you the underlying db types.

Comments

0

Actually, "Date" is a built in data type in CLR, although it is now marked as "obsolete". IDEs do not even show it...

Anyway, as in any case when you want to kwnow the type of an object, I think obj.GetType().Name is not the best way to go, because you end up comparing strings when you actually wanna know if an obj is of ceratin type.

I'd go with

if (dt.Columns[col] is DateTime)
.....
else
  if (dt.Columns[col] is Date) 
   ... 

I am just showing another way of performing datatype checks. You said yourself, it os not possible to compare to "Date".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.