Solved

Convert DateTime String to Datetime

Posted on 2011-03-09
9
708 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 30

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
 
LVL 30

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 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