By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,905 Members | 1,626 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,905 IT Pros & Developers. It's quick & easy.

formatting TextBox.Text with ToString(IFormatProvider)

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
"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

P: n/a
"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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.