Solved

Convert DateTime String to Datetime

Posted on 2011-03-09
9
678 Views
Last Modified: 2012-05-11
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:DateTime  myDateTime=System.Convert.ToDateTime(item.StartDate);

Option 2:

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
0
Comment
Question by:Sha1395
  • 5
  • 2
  • 2
9 Comments
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 35089818
The error you say you're getting with option 2 implies that sampledate is already in system.datetime format
0
 

Author Comment

by:Sha1395
ID: 35089963
even i thought the same but when i pass the value as it is to DB,i gotta an error can't convert type from string to DateTime.

0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 35089970
What code are you using to pass to the database
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 35090291
I believe you problem is simple.

string strDateTime = sampledate;

The sampledate variable is a DateTime variable, I believe, not a string. Therefore you need to convert it to string. E.g.,

string strDateTime =  sampledate.ToLongDateString();
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 35090353
Also, always try to use "TryParse", not just "Parse":
string strDateTime = "10/03/2010";
                DateTime dateToSave;
                bool canParse = DateTime.TryParse(strDateTime,out dateToSave);

                if (canParse)
                {
                    // save the date
                }
                else
                {
                    // show error
                }

Open in new window

0
 

Author Comment

by:Sha1395
ID: 35090467
Thanks for all your help.

Here is my code

when i try to pass here

 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
0
 

Author Comment

by:Sha1395
ID: 35090597
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 ?
0
 

Author Comment

by:Sha1395
ID: 35090622

Right now am using the below code...it get in to "TESTOK"


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");
                }
0
 

Author Comment

by:Sha1395
ID: 35091093
This is the code

DateTime strDateTime = System.Convert.ToDateTime(item.StartDate);
 string conv=System.Convert.ToString (strDateTime); 
DateTime dateToSave; 
dateToSave = DateTime.Parse(conv); 

Open in new window


Here is the below value am getting "dateToSave = {23/03/1992 12:00:00 AM}" but when it pass it to DB am getting error "Error Converting data type Char to datetime"
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now