Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reader.GetValue date value

I am looping out the reader.GetValue from a SQL Server database, all works, however I have a column of data type Date in the table. When I view the data in the table, it shows correctly the date '18-Dec-17' with NO time.

The output of the reader is of this format: '18-Dec-17 12:00:00 AM' AND all records show 12:00:00 AM the same, regardless of the date change.

Could someone let me know whats going on here and is there an option to remove or do I need to regex or replace it?

while (reader.Read())
{
    while (cnt_field <= reader.FieldCount - 1)
    {
        db_output = db_output + "" + reader.GetValue(cnt_field) + " -\t ";
        cnt_field++;
    }//WEND
    cnt_field = 0;
    db_output = db_output + "\n\r";
}//WEND
like image 734
BH7 Avatar asked Feb 20 '26 09:02

BH7


1 Answers

With "" + reader.GetValue(...) you are doing an implicit reader.GetValue(...).ToString().

What you then see is the default representation of a DateTime. dotNET does not have a separate Date type.

So you will have to detect DateTime values inside your loop and apply the desired formatting.

Something like

while (cnt_field <= reader.FieldCount - 1)
{
   object value = reader.GetValue(cnt_field);
   if (value is DateTime)
     db_output += ((DateTime)value).ToShortDateString();  // apply a std/custom Date format
   else   
      db_output += value.ToString(); 

   db_output += " -\t ";
   ....
}
like image 199
Henk Holterman Avatar answered Feb 21 '26 21:02

Henk Holterman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!