Click here to Skip to main content
15,898,010 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
I have a problem in converting the string only to date.
I am getting the date as string from database

ex:
C#
string datestring = "12/10/2012"
Datetime dt = Convert.ToDateTime(datestring);


How to save the string in date field as date, because the previous example is storing the date with time and I don't want the time 00:00:00.

Can you help me please?


Regards,

S. Keshaavan
Posted
Updated 2-Feb-12 5:03am
v3

If you don't want to use the time part of DateTime then just don't.
First read here: String.Format[^]
then go after this: Standard Date and Time Format Strings[^]
after that have some of this: Custom Date and Time Format Strings[^]

Regards,

Manfred
 
Share this answer
 
A dateTime value is just that a Date & Time, if no time is supplied then the 00:00:00 are used by default.

You can either just use the date part of your dt object (dt.date I think)or store your return string as a different type to datetime, such as string.
 
Share this answer
 
Just ignore the tail of the DateTime, because there is no current appropriate type in .net framework, of cause you can define a new class to implement the function you wanted.
 
Share this answer
 
try this code

C#
string datestring = "12/10/2012";
Datetime dt = Convert.ToDateTime(datestring).Date;
 
Share this answer
 
string datestring = "12/10/2012"
string date=Convert.ToDateTime(datestring ).ToString("MM/dd/yyyy");


try this code and try to insert into database

definatly it will work
 
Share this answer
 
v2
Comments
Rajesh Anuhya 3-Feb-12 1:31am    
Edited: Code Tags added.
--RA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900