A beginner question about date format

G

Geri Reshef

Supose I have a control Text1 with todays date (21/12/2004).
How can I show it in a "yyyymmdd" format?
Solution like Text1.Value.Year.tostring() + .....Month.tostring() + .... Day.tostring() seems to be unelegant..
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You can use DateTime.ToString:
DateTime.Today.ToString( "yy/mm/dd" , DateTimeFormatInfo.InvariantInfo );

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Supose I have a control Text1 with todays date (21/12/2004).
How can I show it in a "yyyymmdd" format?
Solution like Text1.Value.Year.tostring() + .....Month.tostring() + ..... Day.tostring() seems to be unelegant..
 
P

pei_world

DateTime.Now.ToString("YYYYMMDD");
please refer to some book, if it give u error!
Supose I have a control Text1 with todays date (21/12/2004).
How can I show it in a "yyyymmdd" format?
Solution like Text1.Value.Year.tostring() + .....Month.tostring() + ..... Day.tostring() seems to be unelegant..
 
J

Joe Mayo

Hi Geri,

Here's something you could try:

string txtDate = "21/12/2004";

System.Globalization.CultureInfo culture =
new System.Globalization.CultureInfo("FR-fr");

string newDate = DateTime.Parse(txtDate, culture).ToString("yyyyMMdd");

Console.WriteLine(newDate);

The code above demonstrates my suggestion of parsing your text value into a DateTime and reformatting it how you want. I'm making the assumption that you need to specify CultureInfo based on the way you formatted the date. It doesn't parse (raises an exception) on my machine, which is set to en-US.

Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

To split, or to regex 4
update problem 1
calendar 1
Excel date format YYYYMMDD 1
Text to date format? 0
Check Date Format 1
Writing to file in ASCII format + one more question 5
Convert a text in a date format 2

Top