473,800 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

formatting TextBox.Text with ToString(IForma tProvider)

I need to take the value of a textbox and format it in a more readable date.
How do you do this? I tried textbox.text.to string("date format string") but
the compiler doesnt like that idea... any ideas?
Jan 22 '08 #1
6 30008
"Andy B" <a_*****@sbcglo bal.netwrote in message
news:uV******** ******@TK2MSFTN GP04.phx.gbl...
>I need to take the value of a textbox and format it in a more readable
date. How do you do this? I tried textbox.text.to string("date format
string") but the compiler doesnt like that idea... any ideas?
A bit dirty, but you could cast it to a date and then format it, e.g.

VB:
CDate(TextBox1. Text).ToString( "dd/MM/yyyy")

C#:
((DateTime)(Tex tBox1.Text)).To String("dd/MM/yyyy");

Jan 22 '08 #2
"Leon Mayne" <le**@rmvme.mvp s.orgwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
C#:
((DateTime)(Tex tBox1.Text)).To String("dd/MM/yyyy");
The above format will result in an ambiguous date, e.g. 03/02/2008

Use "dd MMM yyyy" for guaranteed clarity...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jan 22 '08 #3
Hi... Good idea but when I tried the following line of code I ended up with
this: Compiler Error Message: CS0030: Cannot convert type 'string' to
'System.DateTim e'
My line of code was:
ConfirmStartTim eLabel.Text=((D ateTime)(StartT imeTextBox.Text )).ToString("dd dd,
MMMM d yyyy h:mmtt");

All i need to do is take a string that looks like this: 01/01/2008 12:00 PM
and format it to look like a better date. I have the date format strings I
want already...it's just figuring out how to format the string that way
since it isn't actually a date.


"Leon Mayne" <le**@rmvme.mvp s.orgwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
"Andy B" <a_*****@sbcglo bal.netwrote in message
news:uV******** ******@TK2MSFTN GP04.phx.gbl...
>>I need to take the value of a textbox and format it in a more readable
date. How do you do this? I tried textbox.text.to string("date format
string") but the compiler doesnt like that idea... any ideas?

A bit dirty, but you could cast it to a date and then format it, e.g.

VB:
CDate(TextBox1. Text).ToString( "dd/MM/yyyy")

C#:
((DateTime)(Tex tBox1.Text)).To String("dd/MM/yyyy");

Jan 22 '08 #4

"Andy B" <a_*****@sbcglo bal.netwrote in message
news:eO******** ******@TK2MSFTN GP06.phx.gbl...
Hi... Good idea but when I tried the following line of code I ended up
with this: Compiler Error Message: CS0030: Cannot convert type 'string' to
'System.DateTim e'
My line of code was:
ConfirmStartTim eLabel.Text=((D ateTime)(StartT imeTextBox.Text )).ToString("dd dd,
MMMM d yyyy h:mmtt");

All i need to do is take a string that looks like this: 01/01/2008 12:00
PM and format it to look like a better date. I have the date format
strings I want already...it's just figuring out how to format the string
that way since it isn't actually a date.
So convert it to a date then format the date to a string. That's what Leon
was getting at, but you can't just cast a string to a DateTime, you have to
convert it:

Convert.ToDateT ime(TextBox1.Te xt).ToString("d ate format");

Jan 22 '08 #5
The above format will result in an ambiguous date, e.g. 03/02/2008
What is ambiguous about March 2nd, 2008? ;-)
Jan 22 '08 #6
When I tried this idea, it worked until i applied date formatting. the
string 03/12/2008 3:30PM actually turned out as January 01 00000000
12000AM... any ideas?

"Scott Roberts" <sr******@no.sp am.here-webworks-software.comwro te in
message news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
>
"Andy B" <a_*****@sbcglo bal.netwrote in message
news:eO******** ******@TK2MSFTN GP06.phx.gbl...
>Hi... Good idea but when I tried the following line of code I ended up
with this: Compiler Error Message: CS0030: Cannot convert type 'string'
to 'System.DateTim e'
My line of code was:
ConfirmStartTi meLabel.Text=(( DateTime)(Start TimeTextBox.Tex t)).ToString("d ddd,
MMMM d yyyy h:mmtt");

All i need to do is take a string that looks like this: 01/01/2008 12:00
PM and format it to look like a better date. I have the date format
strings I want already...it's just figuring out how to format the string
that way since it isn't actually a date.

So convert it to a date then format the date to a string. That's what Leon
was getting at, but you can't just cast a string to a DateTime, you have
to convert it:

Convert.ToDateT ime(TextBox1.Te xt).ToString("d ate format");

Jan 22 '08 #7

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

Similar topics

0
1551
by: Kolmi | last post by:
Has anyone come across a solution to this ? To format a value into an Sql Server sql string, I want to use the call Convert.ToString( value, new MyFormatProvider() ); Why I have to do this myself is because for boolean values the sql string should contain 1 or 0 instead of 'true' or 'false' and because the dotnet DateTime has a greater range than Sql Server datetime.
4
1756
by: ChrisB | last post by:
Hello: I was wondering if there might be a more efficient way to handle number formatting over a range of values: switch(DecimalPlaces) { case 1: formattedResult = result.ToString(".0"); break;
3
1675
by: Perecli Manole | last post by:
I want to convert a string "1234123412341234" to "1234-1234-1234-1234". This does not work: String.Format("{0:xxxx-xxxx-xxxx-xxxx}",s) What will? Although I know there are many other solutions, I am only interested in ones that use the new braket {0:} style formating because many web controls require this. Thanks Perry
5
2192
by: Kivak Wolf | last post by:
Hey everyone, I have a textbox in my web page that is going to be used to enter an E-mail into (just plain text, no HTML). Now, this will interact with a SQL database where the contents of the textbox are sent from the SQL database to the textbox, then the user edits it, and then the text inside the textbox is sent back to the SQL database. I did it the same way as if i had used a VarChar instead of a type "text" variable in the SQL...
1
5793
by: Eric Gofoth | last post by:
Hello There doesn't appear to be an iformatprovider for to specify the format to convert a boolean to. i.e. MyBool.ToString("YES/NO") does not work Any workarounds?
8
9181
by: G.Ashok | last post by:
Hi, I have created CultureInfo object and specified required digit grouping in it. The one of the overloaded ToString methods of Decimal type has parameters to format the value with required custom format and a IFormatProvider. I pass a custom format string for positive, negative and Zero (3 sections) and CultureInfo object containing the required DigitGrouping as IFormatProvider. But ToString is not formatting the value using the digit...
3
5169
by: JD | last post by:
I would like to know what is the preferred/best way to format date values in VB.Net. Here is what I have Dim str1 As String str1 = txtDate.Text 'say 2/3/2005 I need 2/3/2005 to be 03FEB2005. Here is what I tried str1 = txtDate.Text.ToString("ddMMMyyyy")
3
5573
by: Que | last post by:
Hi The I wrote the following Code If Not txtInvoice.Text = "" Then txtInvoice.Text = Convert.ToInt32(txtInvoice.Text()).ToString("000000") End If
4
2836
by: Terry | last post by:
I have a TextBox with a date such as 15/01/2006 which I want to cast into a variable as a short date 15/01/06, also I need to cast a time such as 07:30 A.M. into a variable as a short time. What is the best syntax for this please? Regards
0
9694
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
9553
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
10281
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
10039
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
6824
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
5612
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4152
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
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2953
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.