Click here to Skip to main content
15,891,764 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hello All,

I am facing a problem while converting a string to DateTime.
I am doing it like this:
DateTime dt = Convert.ToDateTime(strDate)

I was getting correct result.

But as soon as I changed my system's short date format from 'MM/dd/yy' to 'dd-MMM-yy', I am getting an error as "string is not a valid DateTime format.

I debugged and checked value of strDate and it is a valid DateTime format.
Can anyone tell me how can I handle this change in system's DateTime format.

Additional Code:
I am saving my DateTimePicker value as a string to database and then on form load i am assigning same value to my DateTimePicker..... but if in between i change my system's datetime format i am getting error
//ScheduleEveryStart is a string which is getting saved in to database
clsGlobal.Configuration.ScheduleEveryStart = Convert.ToString(dtpStart.Value);

//load form code
dtpStart.Value = (clsGlobal.Configuration.ScheduleEveryStart == "") ? System.DateTime.Now : Convert.ToDateTime(clsGlobal.Configuration.ScheduleEveryStart);


Thanks,
Nagendra.
Posted
Updated 18-Apr-11 23:52pm
v4
Comments
Pong D. Panda 18-Apr-11 1:54am    
Did mahen25's solution work? If not, post your other code. I dont see any problem with your current code, unless you store your date data in your database as varchar with your old format.
nagendrathecoder 18-Apr-11 2:26am    
Well, you are correct.
I am doing all this in a configuration settings form.
I am saving a DateTimePicker value into database as a string and on form load i am again assigning that database value to DateTimePicker, this is where error is coming.
Pong D. Panda 18-Apr-11 2:48am    
can you add the datetime data that causes an error?
nagendrathecoder 18-Apr-11 2:57am    
Value of ScheduleEveryStart in database is "3/24/2011 12:07:48 PM".
My system's short data format is dd-MMM-yy means today's date is getting displayed as "18-Apr-11".
Dalek Dave 18-Apr-11 3:34am    
Edited for Grammar and Readability.

try this link

ClickHere[^]
 
Share this answer
 
Comments
nagendrathecoder 18-Apr-11 2:24am    
Hi Mahen, thanks for your answer. But this will work only if i know my system's datatime format in advance.
Here, i can change my datetime format to anything, in this case the solution will fail.
Dalek Dave 18-Apr-11 3:34am    
Good Link
Manfred Rudolf Bihy 18-Apr-11 4:38am    
Nice link! 5+
Hi,
have you tried DateTime.Parse method. Theoretically, the method should take care in recognizing the right format - http://msdn.microsoft.com/en-us/library/1k1skd40.aspx[^]
Regards
 
Share this answer
 
Comments
nagendrathecoder 18-Apr-11 2:38am    
Yes, i even tried DateTime.Parse but its giving same error.
The documentation shows culture specific examples.
Mine problem is i am not changing my culture, i am just changing my datetime format.
Dalek Dave 18-Apr-11 3:34am    
Good Answer.
Manfred Rudolf Bihy 18-Apr-11 4:38am    
d'accord! 5+
If you dont need the time you can call your code like this.

string[] strs = s.Split(' ');

if (strs.Length > 1) {
   DateTime dt = Convert.ToDateTime(strs[0]);
}


The idea is strip out the time that causing the conversion error.

OR try this approach

C#
string dateString = "01/31/2011";
string pattern = "d/M/yyy h:mm:ss tt"; //more info below
DateTime parsedDate = DateTime.Now;

if (DateTime.TryParseExact(dateString, pattern, null, DateTimeStyles.None, out parsedDate)){
   //parsedDate
   //ready to be used
   //....
}


Pattern to use[^]


Hope this works.
 
Share this answer
 
v2
Comments
Dalek Dave 18-Apr-11 3:35am    
Seems reasonable.
nagendrathecoder 18-Apr-11 3:40am    
Thanks for answering...... i'll check this solution but i guess i need time component also.
Pong D. Panda 18-Apr-11 3:45am    
if you need the time component, you can always add it to your converted DateTime by calling strs[1], remember that the converted date will default to 0:00:00
Pong D. Panda 18-Apr-11 5:31am    
check my update solution
Manfred Rudolf Bihy 18-Apr-11 4:39am    
Why not? 5+
The best way (I think) is to store Date/Time in database as a Date/Time type column.
When you then retrieve the Date/Time from the database and what to display it for the user. Then you can do DateTime.ToString(...).

Always save you Date/Time in a DateTime class, and when it is time to display it in UI to the user, then convert it.
Do not convert back and forth.

But if you have to save a Date/Time to a string and save it to a database. Then call

DateTime.ToString("s")


The Sortable ("s") Format Specifier[^]


dtpStart.Value = (clsGlobal.Configuration.ScheduleEveryStart == "") ? System.DateTime.Now : DateTime.ParseExact(clsGlobal.Configuration.ScheduleEveryStart, "s", CultureInfo.InvariantCulture);
 
Share this answer
 
v3

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