473,387 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Stringbuilder and Date format

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
Nov 16 '05 #1
6 7775
<Pa******@newsgroup.nospam> wrote in message news:AF**********************************@microsof t.com...
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
Nov 16 '05 #2
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

Pa******@newsgroup.nospam wrote:
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

Nov 16 '05 #3
Paulus,

I think that here is everything described what you ask
http://msdn.microsoft.com/library/de...tringtopic.asp

and the almost impossible to find page
http://msdn.microsoft.com/library/de...ClassTopic.asp

In my opinion should you try to avoid hard coded culture strings as
"dd-MM-yy"

I hope this helps?

Copr
Nov 16 '05 #4
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" wrote:
<Pa******@newsgroup.nospam> wrote in message news:AF**********************************@microsof t.com...
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

Nov 16 '05 #5
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" wrote:
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

Pa******@newsgroup.nospam wrote:
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

Nov 16 '05 #6
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);
<Pa******@newsgroup.nospam> wrote in message
news:58**********************************@microsof t.com...
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" wrote:
<Pa******@newsgroup.nospam> wrote in message news:AF**********************************@microsof t.com...
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

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: tiger | last post by:
Hi, I am runing into problems with the following code, What I am trying to do is display the object (sb) in a textBox, rather than MessageBox. This is even possible? StringBuilder sb = new...
3
by: Mariano | last post by:
Hi, Can a StringBuilder buffer be cleared? I'm confused. It is supposed that the StringBuilder class, can deal with all the memory allocation and deallocation needed for normal string operations...
7
by: Larry Bird | last post by:
I want to use the "replace" in the string builder class. I've imported the "system.text.stringbuilder" class into my project. However, I don't see the method call for...
15
by: Joe Fallon | last post by:
Is concatentaion inside of a Stringbuilder "evil"? Which is the preferred syntax below? Syntax #1 Dim sb As New StringBuilder sb.Append("Line 1" & vbCrLf) sb.Append("Line 2" & vbCrLf)...
1
by: JenHu | last post by:
Hi experts, I received a sample from bank for the header record in the output file (attached at the bottom). I want to use same method to have a stringbuilder to arrange the fix length...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
3
by: John A Grandy | last post by:
I am using a StringBuilder to build a link tag (based on categoryId and categoryName , which are populated elsewhere ) : StringBuilder sb = new StringBuilder(); sb.Append("<a...
7
by: =?Utf-8?B?Q2hyaXN0aWFuIEhhdmVs?= | last post by:
Hi, when I concatenate strings is it faster to use the StringBuilder or the string.Format function? thanks christian
0
gchq
by: gchq | last post by:
Hi there I'm attempting to put together a string that will line up in columns and then insert this into a pdf form! I've managed this before, but for some reason the columns do not line up this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.