DateTime & string format question

  • Thread starter Thread starter Trevor Balcom
  • Start date Start date
T

Trevor Balcom

Hello, I am new to C# and cannot figure out how to do something simple. How
can I build two strings out of a DateTime object? I need one string to
contain the DateTime's date in YYMMDD format. The other string should
contain the DateTime's time in HHMMSS format. Both strings should always
return a length of 6 characters (they should have leading zero if neccessary
to pad each field out to two digits). There should be no delimeter
characters in the strings, just the digits. e.g: "040104" for 1/4/2004,
"135259" for 1:52:59 PM. Any advice will be appreciated.
 
See if this helps:


string s1 = DateTime.Now.ToString("yyMMdd");
string s2 = DateTime.Now.ToString("hhmmss");


HTH
Brian W
 
Be very careful. The formats you chose (short date for example) are subject
to regional formatting rules. For example, in the U.S., March 5 would yeild
"3/5" whereas in most European countries you would get "5/3". If Trevor is
looking for specific placement of those digits, then I would suggest a more
literal format string (like the ones in Brian's post).

-Rob Teixeira [MVP]
 
Yep, good point. In this case, Brian's post would be a better solution.

Thanks for pointing that out Rob :-)

Cheers,

Mun
 
Back
Top