Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Convert DateTime String to Datetime

Posted on 2011-03-09
9
Medium Priority
?
719 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
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!

 
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 2000 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

610 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