Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 5-Feb-13 7:24am
Comments
Abhishek Pant at 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 at 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 at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

CultureInfo ci=new CultureInfo("gu-IN");
Datetime strDate= Datetime.ParseExact("22-12-2012",ci);
 
Thanks
  Permalink  
Comments
Jayanta Chatterjee at 7-Feb-13 10:03am
   
This workssssssssss...
thanks a lot...
AshishChaudha at 7-Feb-13 23:06pm
   
make it solution if it works so that others can refer to the solution
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
Jayanta Chatterjee at 5-Feb-13 22:12pm
   
Thanks Sir, I have same problem. Now You solved my Problem....
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

DateTime _date = Convert.ToDateTime("22-12-2012");
string str = _date.ToString("MM-dd-yyyy");
  Permalink  
Comments
Andreas Gieriet at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Convert your date like below
string date=yourDate.ToString("MM-DD-YYYY");
you can change the date format whatever you want.
Hope this helps
  Permalink  
v2
Comments
Andreas Gieriet at 5-Feb-13 20:36pm
   
My 2. It's not about ToString but rather "from" string. This is not answering the question.
Andi
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Try this :-
 DateTime shortDate = new DateTime();
 
                System.Globalization.DateTimeFormatInfo dtfi = new                                                                           System.Globalization.DateTimeFormatInfo();
 
            dtfi.ShortDatePattern = "MM-dd-yyyy";
 
            shortDate = DateTime.ParseExact(yourDate, "d", dtfi);
 
Thanks
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 6,745
1 OriginalGriff 6,696
2 CPallini 5,315
3 George Jonsson 3,599
4 Gihan Liyanage 2,650


Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 6 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100