Solved

Convert DateTime String to Datetime

Posted on 2011-03-09
9
687 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

777 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