473,770 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Cou nt; curCol++)
{
sb = sb.Append(dt1.R ows[curRow][curCol].ToString().Tri m()).Append(del im);
}
sb.Append(Envir onment.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 7826
<Pa******@newsg roup.nospam> wrote in message news:AF******** *************** ***********@mic rosoft.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.R ows[curRow][curCol].ToString().Tri m()).Append(del im);


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.Row s[curRow][curCol]; // if its a DateTime field

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

HTH
Marcin

Pa******@newsgr oup.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.Cou nt; curCol++)
{
sb = sb.Append(dt1.R ows[curRow][curCol].ToString().Tri m()).Append(del im);
}
sb.Append(Envir onment.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******@newsg roup.nospam> wrote in message news:AF******** *************** ***********@mic rosoft.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.R ows[curRow][curCol].ToString().Tri m()).Append(del im);


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(((Dat eTime) dateObj).ToShor tDateString());

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.Row s[curRow][curCol]; // if its a DateTime field

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

HTH
Marcin

Pa******@newsgr oup.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.Cou nt; curCol++)
{
sb = sb.Append(dt1.R ows[curRow][curCol].ToString().Tri m()).Append(del im);
}
sb.Append(Envir onment.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******@newsg roup.nospam> wrote in message
news:58******** *************** ***********@mic rosoft.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******@newsg roup.nospam> wrote in message news:AF******** *************** ***********@mic rosoft.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.R ows[curRow][curCol].ToString().Tri m()).Append(del im);
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
5261
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 StringBuilder(); sb.Append("Payment paid to: " + myPayment.Payor); sb.Append("\n");
3
3520
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 (concatenations, replacements, etc.), because the String class just creates and destroys instances at an object level. I can't find a method to clear a StringBuilder object's buffer for reusing it (the idea is to avoid object...
7
3375
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 "replace(oldchar,newchar,startindx,numofchars)" format. It does not show up in intellisense. What am I doing wrong. See below: imports system.text.stringbuilder Thanks
15
1604
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) sb.Append("Line 3" & vbCrLf)
1
3451
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 positions, and the fields will be retrieved from database and put into the dataset. But I am familiar at how to use dataset and put into .insert stringbuilder. Here is the code that I am working on (the function should return 1 string line at a time....
33
4690
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 of articles I read from different experts and programmers tell me that their "gut feelings" for using stringBuilder instead of string concatenation is when the number of string concatunation is more then N ( N varies between 3 to max 15 from...
3
11856
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 href=\"http://www.mywebsite.com/mypage.aspx?category="); sb.Append(categoryId); sb.Append("&filter=price\">"); sb.Append(categoryName); sb.Append("</a>"); html += sb.ToString();
7
335
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
1795
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 time - been over every inch of the code and cannot fnd a reason - if anyone can spot the obvious I'd really appreciate it! Dim vInfo As New System.Text.StringBuilder() vInfo.AppendFormat("{0, -14} {1, 30} {2, 30}", "Earnings", "This...
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9453
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10254
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.