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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting Dates

Status
Not open for further replies.

yu217171

Programmer
Joined
Aug 2, 2002
Messages
203
Location
CA
Hi everyone,

Just a simple question which I'm sure all of you can answer. How do I convert a date in the form MM/DD/YYYY to DD/MM/YYYY?

Thanks in advance,

Keith
 
Keith: There is a series of formatting Date/time codes, e.g.,

D: long date
d: short date
f: long date, short time
...
... and so forth, e.g.,

Respsonse.Write(dtmDate.ToString("d"))

..or using a formating statement:

Response.Write(String.Format({0:D}, DateTime.Now))
--> April 12, 2004

...in a DataGrid column you wight use:

DataFormatString="{0:dd MMM yyyy}"

..do a quick search here at Tek-Tips, lots of threads on formatting ---
 
Thanks I got it to work.

Code:
strDate = "01/31/2004";
DateTime myDate = DateTime.Parse (strDate);
myDate = myDate.ToString ("MM/dd/yyyy");

Jumped in from C++ and Java. C# is very similiar =)

Keith

 
good luck Kieth, hope to see you around -- Tek Tips is a great place for technical discussions - there are quite a few here who are truly 'wizards' at this stuff... so well worth the visit.
 
Thanks for the warm welcome. I'll be sure to hang around and learn a lot.

Keith
 
Keith:
I liked your solution and tried it.
Is there a typo because you can not implicitly convert a DateTime to a string. Did I miss something?
Marty
 
What's the error that you get?

myDate = myDate.ToString ("MM/dd/yyyy");

That above line should work fine. I'm using C# syntax. You may have to modify your object declarations with VB (if in fact that is what you're using).

Keith
 
Keith:
Here is the code
Code:
string strDate = "01/31/2004";
DateTime myDate = DateTime.Parse (strDate);
myDate = myDate.ToString ("MM/dd/yyyy");
Console.WriteLine("myDate " + myDate);
Here is the error
error CS0029: Cannot implicitly convert type 'string' to
'System.DateTime'

This is the offending line which makes sense to me
myDate = myDate.ToString ("MM/dd/yyyy");

Marty
 
string strDate = "01/31/2004";
DateTime myDate = DateTime.Parse (strDate);
string NewDate = myDate.ToString ("dd/MM/yyyy");
Console.WriteLine("NewDate " + myDate);

Sorry it was indeed a typo.

Keith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top