-2

I have a string stored in table "13/12/1985 12:00:00 a.m." when i tried to convert this to Datetime, i am getting an exception saying "String is not a valid DateTime Format". It is because the first part of the string(13) is month. Is there any way to convert the above string to (mm/dd/yyyy hh:mm:ss am/pm) format?. Actually the string saved in table is in the format of "dd/mm/yyyy". I want to convert to "mm/dd/yyyy" in Datetime

user3403111
  • 65
  • 2
  • 9
  • Another question: why do you store a `DateTime` as `string` in your table? You would avoid this exception and other issues if you'd store it as what it is. – Tim Schmelter May 02 '16 at 12:32
  • 1
    @DHN, that link is for `python`, but this question is for `C#`. I'm positive there's answers for C# though – Berin Loritsch May 02 '16 at 12:34
  • @TimSchmelter that is an existing table. And that table has seperate column to store Datatype. Say DateTime as 'D'. If it is D then i need to convert to datetime – user3403111 May 02 '16 at 12:40
  • @user3403111: why do you store the type in a separate column? Don't store everything as string/nvarchar and convert it clientside. Instead use the correct type in the first place. – Tim Schmelter May 02 '16 at 12:41
  • So you've built a database engine in a database engine? – Lasse V. Karlsen May 02 '16 at 12:42

1 Answers1

1

Try this, its convert dd/MM/yyyy hh:mm:ss tt format datetime to MM/dd/yyyy hh:mm:ss tt format

DateTime dt = DateTime.ParseExact("26/04/2016 12:00:00 PM", "dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
string newdate = dt.ToString("MM/dd/yyyy hh:mm:ss tt")
Mostafiz
  • 7,243
  • 3
  • 28
  • 42