Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am getting date time in string format(dd/mm/yyyy). I want to convert to it in to datetime. So i tried bellow methods which failed. So please help me regarding this issue.
C#
//datetime in string format from client

string mindate = 15/06/2013 11:03:24
 //now i am converting above to DateTime

DateTime MinDateTime = Conevrt.ToDateTime(mintime)//getting error Message = "String was not recognized as a valid DateTime."

// another conversion

DaTeTime MinDateTime = DateTime.ParseExact(mintime)//getting error Message = "String was not recognized as a valid DateTime."

// another conversion


DaTeTime MinDateTime = DateTime.Parse(mintime)//getting error Message = "String was not recognized as a valid DateTime."

so please help me regarding the conevrsions. Thank You.
Posted
Updated 20-Jun-13 21:53pm
v2

The problem is with the data. It is considering mm/dd/yyyy format instead of dd/mm/yyyy. Try to swap the month and day part in your mindate variable.
 
Share this answer
 
v2
Check this :
C#
string[] dateStrings = {"2008-05-01T07:34:42-5:00",
                        "2008-05-01 7:34:42Z",
                        "Thu, 01 May 2008 07:34:42 GMT"};
foreach (string dateString in dateStrings)
{
   DateTime convertedDate = DateTime.Parse(dateString);
   Console.WriteLine("Converted {0} to {1} time {2}.",
                     dateString,
                     convertedDate.Kind.ToString(),
                     convertedDate);
}
 
Share this answer
 
Try It i think you can get your solutions.
Convert.ToDateTime(mindate).ToString("dd'.'MM'.'yyyy") 

Thanks
 
Share this answer
 
v2
use this

string dateString = "15/06/2008 08:30";
string format = "g";
CultureInfo provider = new CultureInfo("fr-FR");

DateTime result = DateTime.ParseExact(dateString, format, provider);


Use using System.Globalization; top of the page.
Also instead og g you can use
DateTime result = DateTime.ParseExact(dateString, "dd/MM/yyyy HH:mm", provider);
 
Share this answer
 
v2
Comments
kalyan10qwerty 21-Jun-13 4:20am    
What is g and i am able to get only culture but not cultureinfo.
C#
string mindate = 15/06/2013 11:03:24;
string dateformat = "MMMM-dd-yyyy hh:mm:ss";
DateTime.Parse(mindate.ToString(dateformat));
 
Share this answer
 
v2
Comments
kalyan10qwerty 21-Jun-13 4:23am    
string mindate = 15/06/2013 11:03:24
string DformatMin = "dd/mm/yyyy HH:mm:ss";
datetime kd1 = DateTime.Parse(mindate.tostring(DformatMin));
Error 1 The best overloaded method match for 'System.DateTime.Parse(string, System.IFormatProvider)' has some invalid arguments
Nripendra Ojha 21-Jun-13 4:34am    
Nripendra Ojha - 7 mins ago
string DformatMin = "dd/mm/yyyy HH:mm:ss"; please use capital M not small string DformatMin = "dd/MMMM/yyyy HH:mm:ss";

please inform me. if it will not run or run.
kalyan10qwerty 21-Jun-13 4:43am    
string DformatMin = "dd/MMMM/yyyy HH:mm:ss";

DateTime kd1 = DateTime.Parse(Mintime.ToString(DformatMin));

It is showing same error.
Nripendra Ojha 21-Jun-13 4:49am    
string DformatMin = "dd/MMMM/yyyy HH:mm:ss";
there is again a small mistake in DformatMIn. You use capital H. i am also not notice that sorry for that. for ur knowledge know the meaning of

dd small d is for day
MM capital M for month
yy small y for year

hh small h for hour
mm small m for minute
ss small s for second

please use this code

string DformatMin = "dd/MMMM/yyyy hh:mm:ss";
DATETIME kd1 = DateTime.Parse(Mintime.ToString(DformatMin));
kalyan10qwerty 21-Jun-13 4:56am    
I have changed capital H to small h but again the same result.Please help meeeeee
The Problem is with system culture.
it is following US-EN culture.
If you pass your string in "mm/dd/yyyy Time" instead of "dd/mm/yyy Time", it will work.
if your string in not coming in US-EN format. then you have to override the default culture and set it to you culture.
Below link can of your help
String was not recognized as a valid DateTime.[^]
 
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