Stringbuilder and Date format

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am writing a DataTable to a file using a stringbuilder.

The Format of the date in the table is like this: '12/20/04'

But after I write it to the file it ads the time: '12/20/04 12:00:00 AM'

My code looks like this

for(int curRow = 0; curRow < dt1.Rows.Count; curRow++)
{
for(int curCol = 0; curCol < dt1.Columns.Count; curCol++)
{
sb = sb.Append(dt1.Rows[curRow][curCol].ToString().Trim()).Append(delim);
}
sb.Append(Environment.NewLine);
}
f.WriteLine(sb.ToString());
f.Close();

I know there is something like .toshortdate, but I don't know where to put it.

ANy ideas

Thanks

PAul
 
The Format of the date in the table is like this: '12/20/04'
But after I write it to the file it ads the time: '12/20/04 12:00:00 AM' : :
sb = sb.Append(dt1.Rows[curRow][curCol].ToString().Trim()).Append(delim);

if ( dt1.Columns[curCol].DataType == typeof( DateTime) )
sb.Append( dt1.Rows[ curRow][ curCol].ToString( "d").Trim( ));
else
sb.Append( dt1.Rows[ curRow][ curCol].ToString( ).Trim( ));
sb.Append( delim);


Derek Harmon
 
Hi,

object dateObj=dt1.Rows[curRow][curCol]; // if its a DateTime field

if( dateObj!=DBNull.Value ) {
sb.Append(((DateTime) dateObj).ToShortDateString());
// or
// sb.AppendFormat("dd/MM/yy", dateObj); // if i'm not wrong
}

HTH
Marcin
 
This did not work when used in conjunction with a stringbuilder.
Error 'No overload for method 'ToString' takes '1' Arguments.

See thread from Marcin.

Thanks

Derek Harmon said:
The Format of the date in the table is like this: '12/20/04'
But after I write it to the file it ads the time: '12/20/04 12:00:00 AM' : :
sb = sb.Append(dt1.Rows[curRow][curCol].ToString().Trim()).Append(delim);

if ( dt1.Columns[curCol].DataType == typeof( DateTime) )
sb.Append( dt1.Rows[ curRow][ curCol].ToString( "d").Trim( ));
else
sb.Append( dt1.Rows[ curRow][ curCol].ToString( ).Trim( ));
sb.Append( delim);


Derek Harmon
 
Thank You

This way: sb.Append(((DateTime) dateObj).ToShortDateString());

Worked

This way: sb.AppendFormat("dd/MM/yy", dateObj);

Did not, it actually added the literal string 'dd/MM/yy' to the file. I also
tried "dd-MM-yy" and "d".

I'll use the first one, it works just fine.

Thank you.

Paul


Marcin Grzębski said:
Hi,

object dateObj=dt1.Rows[curRow][curCol]; // if its a DateTime field

if( dateObj!=DBNull.Value ) {
sb.Append(((DateTime) dateObj).ToShortDateString());
// or
// sb.AppendFormat("dd/MM/yy", dateObj); // if i'm not wrong
}

HTH
Marcin

I am writing a DataTable to a file using a stringbuilder.

The Format of the date in the table is like this: '12/20/04'

But after I write it to the file it ads the time: '12/20/04 12:00:00 AM'

My code looks like this

for(int curRow = 0; curRow < dt1.Rows.Count; curRow++)
{
for(int curCol = 0; curCol < dt1.Columns.Count; curCol++)
{
sb = sb.Append(dt1.Rows[curRow][curCol].ToString().Trim()).Append(delim);
}
sb.Append(Environment.NewLine);
}
f.WriteLine(sb.ToString());
f.Close();

I know there is something like .toshortdate, but I don't know where to put it.

ANy ideas

Thanks

PAul
 
The string builder is not the problem. It's just that Derek forget that
while we know that the object returned from dt1.Rows[ curRow][ curCol] is
actually a DateTime, it's really just an Object.

object cell = dt1.Rows[ curRow][ curCol];
if ( dt1.Columns[curCol].DataType == typeof( DateTime) )
sb.Append( ((DateTime) obj).ToString( "d").Trim( ));
else
sb.Append( obj.ToString( ).Trim( ));
sb.Append( delim);


This did not work when used in conjunction with a stringbuilder.
Error 'No overload for method 'ToString' takes '1' Arguments.

See thread from Marcin.

Thanks

Derek Harmon said:
The Format of the date in the table is like this: '12/20/04'
But after I write it to the file it ads the time: '12/20/04 12:00:00
AM'
: :
sb.Append(dt1.Rows[curRow][curCol].ToString().Trim()).Append(delim);

if ( dt1.Columns[curCol].DataType == typeof( DateTime) )
sb.Append( dt1.Rows[ curRow][ curCol].ToString( "d").Trim( ));
else
sb.Append( dt1.Rows[ curRow][ curCol].ToString( ).Trim( ));
sb.Append( delim);


Derek Harmon
 
Back
Top