Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to chnage date format from dd-MM-yy to MM-dd-yy in asp.net

I have date "22-12-2012"
i want to change into "12-22-2012"

when i try to convert it gives me error

String was not recognized as a valid DateTime.
Posted
Comments
Abhishek Pant 5-Feb-13 13:28pm    
SELECT CONVERT(VARCHAR(10), GETDATE(), 110) AS [MM-DD-YYYY]
for more help http://www.sql-server-helper.com/tips/date-formats.aspx
Sergey Alexandrovich Kryukov 5-Feb-13 13:30pm    
Who told you it's SQL?
Again, the same boring question...
I answered in full; and the most important thing is avoiding "convert" approach; please see.
—SA
z3ngew 5-Feb-13 14:13pm    
Hello Sergey, I'm sorry that i have contacted you like that, i would like to take your opinion in my work please take a look at it
http://www.youtube.com/watch?v=tlH0-o2fAcI&feature=youtu.be

raise the volume of your speakers
Thanks for your support
Sergey Alexandrovich Kryukov 5-Feb-13 14:35pm    
And why? Speech synthesis, available in System.Speech.Synthesis, for example... you can do it right away...
(Sorry, I though you are the OP.., confused...)
—SA

C#
CultureInfo ci=new CultureInfo("gu-IN");
Datetime strDate= Datetime.ParseExact("22-12-2012",ci);


Thanks
 
Share this answer
 
Comments
JayantaChatterjee 7-Feb-13 10:03am    
This workssssssssss...
thanks a lot...
AshishChaudha 7-Feb-13 23:06pm    
make it solution if it works so that others can refer to the solution
First of all, I don't think you really need to "convert". You should not work with string representation of data instead of data. In your case, you should work with System.DateTime instead of strings representing it. Only of you need to show the data on screen, you use one of System.DateTime methods, and only then you choose the format. Please see:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^], read on every ToString method.

For formatting, please see
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].

Another way of formatting is through choosing appropriate culture, see, for example:
http://msdn.microsoft.com/en-us/library/ht77y576.aspx[^].

In rare cases, you need to interpret string from certain format. Even this can be simplified, because, say, in SQL you should better use DATE type not tied to a particular format, in contrast to strings. But in case you need, you should parse a string into System.DateTime, so see all the methods Parse, ParseExact, TryParse and TryParseExact.

Of course, using the methods referenced, you can "convert" as you wanted, but "convert" approach is wrong.

—SA
 
Share this answer
 
Comments
JayantaChatterjee 5-Feb-13 22:12pm    
Thanks Sir, I have same problem. Now You solved my Problem....
C#
DateTime _date = Convert.ToDateTime("22-12-2012");
string str = _date.ToString("MM-dd-yyyy");
 
Share this answer
 
Comments
Andreas Gieriet 5-Feb-13 20:35pm    
My 3. This will not work on all cultures. how could the ToDateTime know or even guess which is the year, mont, day part of that string?
Andi
Convert your date like below
C#
string date=yourDate.ToString("MM-DD-YYYY");

you can change the date format whatever you want.
Hope this helps
 
Share this answer
 
v2
Comments
Andreas Gieriet 5-Feb-13 20:36pm    
My 2. It's not about ToString but rather "from" string. This is not answering the question.
Andi
Try this :-
C#
DateTime shortDate = new DateTime();

               System.Globalization.DateTimeFormatInfo dtfi = new                                                                           System.Globalization.DateTimeFormatInfo();

           dtfi.ShortDatePattern = "MM-dd-yyyy";

           shortDate = DateTime.ParseExact(yourDate, "d", dtfi);


Thanks
 
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