Solved

Convert DateTime String to Datetime

Posted on 2011-03-09
9
668 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

13 Experts available now in Live!

Get 1:1 Help Now