Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DateTime 1

Status
Not open for further replies.

sand133

Programmer
Jun 26, 2004
103
GB
hi,

I have a string "2002-11-02 16:26:04.203".

How to do i format this to to mm/dd/yyy, THis is getting use everywhere in my app, so a common easy function was be nice.
I know in VB it was easy FormatDate...
any ideas

thanks
 
sorry this needs to be strongly typed to a DateTime format.
 
You could write your own function and use a regex:

string input = "2002-11-02 16:26:04.203";
string output;
//4 year date
output = Regex.Replace(input,@"(\d{4})-(\d{2})-(\d{2})(.*)","$2/$3/$1");
Console.WriteLine("output mm/dd/yyyy: " + output);
//2 year date
output = Regex.Replace(input,@"(\d{2})(\d{2})-(\d{2})-(\d{2})(.*)","$3/$4/$2");
Console.WriteLine("output mm/dd/yy: " + output);

Marty
 
Forgot the output:
output mm/dd/yyyy: 11/02/2002
output mm/dd/yy: 11/02/02
Marty
 
DateTime somedate = DateTime.Parse("2002-11-02 16:26:04.203");

somedate.ToString("MM/dd/yyy hh:mm:ss");


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top