Thursday, March 22, 2012

Simple Date Formatting Question

Everywhere I've looked regarding formatting a date shows formatting a DateTime type. I am trying to format a date that has been set into a string type. I trying to get the following format: mm/dd/yyyy (2 digit month, 2 digit day and 4 digit year).
How can I do this?
I know it's probably extremely simple, but everything I've tried leaves the date as: 1/5/2005 instead of 01/05/2005.
Thanks,
Mark

DateTime.Now.ToString("MM/dd/yyyy")


The field I'm working with is astring type not a datetime.
string dueDate;
The "dueDate" field is populated from the database with:
1/5/2005
How do you format a "string" that contains a date with a given format. Like I said, everything on the internet shows the DateTime.Now.ToString(...) that you've shown. My question is regarding a string that contains a date.
Thanks,
Mark


If your converting it to a string specifically to format it, you can avoid loosing the datetime features of that field by just using a Format([DateTime[), "MM/dd/yyyy")

string dateText = "1/5/2005";
DateTime dateTimeValue = System.DateTime.Parse(dateText);

string formattedValue = dateTimeValue.ToString("MM/dd/yyyy");
this.Response.Write("formattedValue: " + formattedValue + "<br>");
NC...

0 comments:

Post a Comment