Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have string in that I have set date as
string date="07/02/2011" as dd/mm/yyyy format.
when I am converting it in date format it's changing in "07/02/2011" but i have to change it in 02/07/2011.

Please help me
Thanks
Mohd Wasif
Posted

This will help you

string date="07/02/2011"
Respose.Write(string.format("{0:dd/MM/yyyy}",date));
 
Share this answer
 
Comments
Mohd Wasif 2-Feb-11 8:01am    
CS0117: 'string' does not contain a definition for 'format'

showing this error
soni uma 2-Feb-11 8:13am    
Have you type string. in your vs editor? Because it is impossible that string does not contain definition for format. Check again it is format or Format.
Mohd Wasif 2-Feb-11 8:14am    
It's again giving me 07/02/2011 but i want in 02/07/2011.
soni uma 2-Feb-11 8:30am    
use this one it is working perfect

string date = "07/02/2011";
Response.Write(string.Format("{0:dd/MM/yyyy}", Convert.ToDateTime(date)));
It isn't "changing in" "07/02/2011" because you just did cast a string to date, but that would be the default representation of a date. The representation is "MM/dd/yyyy" and cannot logically be changed due to possible errors in interpretation because it is unclear if 07 is the month or the day (this also goes for 02 in the date given).

I myself am from the netherlands and the representation there is using a dash(-) instead of a slash(/) and the date is starting with the day instead of the month. So "07/02/2011" would become "02-07-2011". The slash and dash keep it clear what the first two values represent (either day or month). Without that there is really no way to know except when the day is above 13 because there is no 13th month.

So, could you please explain what the reason is to reverse day and month? If you really want to, you can just use the Split method like:
string [] split = date.Split(new Char [] {'\\'});
// You should need to check the lower and upper bound of the array 
string newDate = split[1] + split[0] + split[2];


You could also use a custom formatting:
string newDate = String.Format("{0:dd/MM/yyyy}", dt); // dt is of type DateTime


Good luck!
 
Share this answer
 
v2
Comments
Manas Bhardwaj 2-Feb-11 10:56am    
very nicely explained. +5
You can get help from this link:

http://www.csharp-examples.net/string-format-datetime/[^]
 
Share this answer
 
try this simple soln:-
string DateString  = "07/02/2011";
DateTime date = new DateTime();
date = DateTime.ParseExact(DateString, "dd/MM/yyyy");
string NewDateString = date.ToString("MM/dd/yyyy");


or
var result = DateTime.ParseExact("07/02/2011", "MM/dd/yyyy", System.Globalization.CultureInfo.CurrentCulture);
 
Share this answer
 
Comments
Karthik_Mahalingam 25-Jan-14 14:06pm    
5
TrushnaK 26-Jan-14 23:57pm    
Thanks karthik...
string test = "07/02/2010";
        DateTime dt123 = DateTime.ParseExact(test, "dd/MM/yyyy",null);
        Response.Write(dt123.ToString());

This has been answered around a week back here[^] and for you.
 
Share this answer
 

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