473,320 Members | 1,916 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,320 software developers and data experts.

formatting TextBox.Text with ToString(IFormatProvider)

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.tostring("date format string") but
the compiler doesnt like that idea... any ideas?
Jan 22 '08 #1
6 29929
"Andy B" <a_*****@sbcglobal.netwrote in message
news:uV**************@TK2MSFTNGP04.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.tostring("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)(TextBox1.Text)).ToString("dd/MM/yyyy");

Jan 22 '08 #2
"Leon Mayne" <le**@rmvme.mvps.orgwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
C#:
((DateTime)(TextBox1.Text)).ToString("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.DateTime'
My line of code was:
ConfirmStartTimeLabel.Text=((DateTime)(StartTimeTe xtBox.Text)).ToString("dddd,
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.mvps.orgwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
"Andy B" <a_*****@sbcglobal.netwrote in message
news:uV**************@TK2MSFTNGP04.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.tostring("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)(TextBox1.Text)).ToString("dd/MM/yyyy");

Jan 22 '08 #4

"Andy B" <a_*****@sbcglobal.netwrote in message
news:eO**************@TK2MSFTNGP06.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.DateTime'
My line of code was:
ConfirmStartTimeLabel.Text=((DateTime)(StartTimeTe xtBox.Text)).ToString("dddd,
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.ToDateTime(TextBox1.Text).ToString("date 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.spam.here-webworks-software.comwrote in
message news:%2****************@TK2MSFTNGP06.phx.gbl...
>
"Andy B" <a_*****@sbcglobal.netwrote in message
news:eO**************@TK2MSFTNGP06.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.DateTime'
My line of code was:
ConfirmStartTimeLabel.Text=((DateTime)(StartTimeT extBox.Text)).ToString("dddd,
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.ToDateTime(TextBox1.Text).ToString("date format");

Jan 22 '08 #7

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

Similar topics

0
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...
4
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");...
3
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...
5
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...
1
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
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...
3
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...
3
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
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.