Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi i have a value coming from external

Am getting string value (07/03/2011 12:00:00 AM ) and i want to convert to DateTime and update the Database.
At present am trying below methods but still no go...

Option 1:
C#
DateTime  myDateTime = System.Convert.ToDateTime(item.StartDate);

Option 2:
C#
string strDateTime = sampledate; //(when i use this "it shows Cannot implicitly convert type system.DateTime ? to String)
DateTime myDateTime = DateTime.Parse(strDateTime );

Please some one guide me what is the right way to convert


Update 1:

The below code is working

C#
DateTime strDateTime = System.Convert.ToDateTime(item.StartDate); 
string conv=System.Convert.ToString (strDateTime); 
DateTime dateToSave; 
bool canParse = DateTime.TryParse(conv, out dateToSave); 

if (canParse)
 {
 Console.WriteLine("Testok");
 } 
else
 {
 Console.WriteLine("Fail");
 }
Posted
Updated 9-Mar-11 17:01pm
v3

Use one of the overloaded methods System.DateTime.Parse or System.DateTime.ParseExact.

—SA
 
Share this answer
 
Comments
shan1395 9-Mar-11 20:54pm    
Thanks for your comment,

changed the code like below

string strDateTime = item.StartDate (can't implictly convert type system.datetime?tostring
DateTime myDateTime = System.DateTime.Parse(strDateTime);

still throwing error (
Sergey Alexandrovich Kryukov 9-Mar-11 21:28pm    
OK. For a developer, it throws exception, not error. This is simple. Look at the API properly, see what form of format is expected. Either change format on input or use proper format provider. You did not share a sample of your string, so I did not know what are you trying to do. Work with format closely and you will find how to do it properly.
--SA
shan1395 9-Mar-11 22:41pm    
thanks for your comment ,sample string in my question anyway.

This is the value am getting for item.startdat =07/03/2011 12:00:00 AM
Sergey Alexandrovich Kryukov 9-Mar-11 23:16pm    
If you said 30/03/2011 or 03/30/2011 it would be clear :-) Find out what culture is this and proceed. This is all what's involved.
--SA
Use DateTime.TryParse method.

This is how it is done:

C#
DateTime dateTime;

DateTime.TryParse(strDateTime, out dateTime);


The coolest part is TryParse will return True if the parsing is successful and return False if the parsing fail. You can either use the statement in a if condition.


Update 1:

shan1395 wrote:
DateTime strDateTime = System.Convert.ToDateTime(item.StartDate);


This code is one of the ways to convert string to DateTime. It is not required to use TryParse if you use Convert.ToDateTime

Convert.ToDateTime will fail and throw exception if the string is empty. But TryParse will not throw exception.

Solution 1:
C#
DateTime dateTime = System.Convert.ToDateTime(item.StartDate);


or

Solution 2:
C#
DateTime dateTime;

DateTime.TryParse(item.StartDate, out dateTime);


Mark it as answer if it is helpful
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 9-Mar-11 21:29pm    
I already answered. You "try" does not change anything. It will return false, what's next?
--SA
Venkatesh Mookkan 9-Mar-11 21:37pm    
Did you tried mine? When I answered the question I simply gave the code without trying it out as I am using it daily. But Once you replied, I tried it and it is parsing perfectly (return True). You should check your answer. You are suggesting Parse and I am suggesting TryParse.

TryParse will never throw exception even the date format is wrong. If it return False, then the date is not valid. What else can we do then? It is up to the developer or the business requirement to figure that out if the date is not valid.
Sergey Alexandrovich Kryukov 9-Mar-11 22:33pm    
Of course I no that. Now, why not throwing exception?
--SA
Venkatesh Mookkan 9-Mar-11 22:43pm    
This might help http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx
shan1395 9-Mar-11 22:51pm    
nothing worked me...but if i use the below code

DateTime strDateTime = System.Convert.ToDateTime(item.StartDate); its not showing any error

but i couldn't understand the value am getting is string or already am getting date time ?

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