Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,

I have a requirement where i need to changes the date format from dd/mm/yyyy to mm/dd/yyyy and vice versa.

I have a problem with dates having 12/13/2006 or 13/12/2007. That means there is a problem of converting 12 and 13. Can anyone please help me on this how to resolve?

dataGridView1.Rows[rowindex].Cells[index].Value = Convert.ToDateTime(Column[i]).ToString(StaticVar.DateFormat);

What I have tried:

I tried changing the format in multiple times but didn
t worked.
Posted
Updated 27-Jun-16 18:01pm

1 solution

C#
DateTime dt = DateTime.ParseExact("13/12/2006", "dd/MM/yyyy", CultureInfo.InvariantCulture);
string dtNew = dt.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);

For the problem "12/13" or "13/12", you must know, in what cultureinfo you are right now. So that you can convert it in the right way. If you want to catch exception, that is also not possible, because these 2 dates are OK : 1/2 or 2/1 -> Jan 2nd or Feb 1st
 
Share this answer
 
Comments
Member 8010354 28-Jun-16 2:51am    
Actually i have a file where the date field will have multiple dates. So if it is in dd/mm, i have to change it to mm/dd. If it is in mm/dd, i have to change it to dd/mm. So that is what my task is.

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