A beginner question about date format

  • Thread starter Thread starter Geri Reshef
  • Start date Start date
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..
 
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..
 
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..
 
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
 
Back
Top