Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
i have a a value = "9/1/2001" in a array[]
i need to convert it to "09-01-2001"

please help
Posted
Comments
Santhosh Kumar Jayaraman 16-Aug-12 6:40am    
Is it string array or datetime array?

Hi,
try this to convert your string into mmddyyyy format
C#
DateTime.ParseExact(yourDateString, "ddMMyyyy", CultureInfo.InvariantCulture);


Best Luck
Happy Coding
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Aug-12 2:37am    
This is just parse, not convert, but I don't think it's really needed. OP is confused and needs the explanation.
Please see my answer.
--SA
There are two things you'll have to do. First off all convert your string to a DateTime:
C#
string dateString = "9/1/2001";
DateTime dateValue;
if(DateTime.TryParse(dateString, out dateValue)){
   // Convert is ok
}
else{
   // Could not convert
   // The string contains a invalid date or invalid dateformat
}


Then if the convert is ok, you can format it the way you need

C#
string formatedDate = dateValue.ToString("dd-MM-yyyy");


List of date format patterns: http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 16-Aug-12 2:38am    
The root of the problem is: "conversion" is hardly really needed. Please see my answer.
--SA
StianSandberg 16-Aug-12 2:51am    
I totally agree with you! It's important to keep data AS data. As I can see in rohit24c491's question he has a string value "9/1/2001" (which should be a date) and wants to convert it to another dateformat. That is why my answer is that he will have to do 2 things. First make sure "9/1/2001" is a DateTime object. Then he can format the DateTime object as he prefer.
You have a number of answers, but your root problem is different. It try to work with string representation of data, instead of data itself. This is wrong. Work with data, in this case, System.DateTime data. You don't usually need to convert one string into another. You need to work with data all the time until you need to present it on screen. In that case, use one of the System.DateTime.ToString functions. Yes, as simple as that.

—SA
 
Share this answer
 
Date dt="9/1/2001";
String s=dt.ToString("dd-MM-yyyy");
 
Share this answer
 
Comments
StianSandberg 16-Aug-12 2:15am    
This is not a valid C# syntax.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900