Click here to Skip to main content
15,910,123 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
dateFrom = Convert.ToDateTime(dr["DateFrom"].ToString());

How do I retrieve as Date only?
Posted
Updated 1-Feb-14 19:33pm
v5
Comments
Karthik_Mahalingam 31-Jan-14 6:01am    
what issue you are facing >?
Member 10548723 31-Jan-14 6:01am    
When I retrieve from database, is datetime when I stored it as Date.
Pete O'Hanlon 31-Jan-14 6:06am    
Please stop raising the same question over and over again. Engage in a discussion with the people who have already answered your questions in other threads if you have a problem. As it stands, people will soon start deleting your duplicate questions.

So, the problem is with formatting the DateTime?
Just specify the format string explicitly when converting to a string using .ToString:
C#
string formattedDate = Convert.ToDateTime(dr["DateForm"].ToString()).ToString("dd/MM/yyyy");
 
Share this answer
 
v2
See , if your SQL Column DataType is Date it will store as "01/01/2001" (time stamp will not be included)
if it is DateTime it will include Date and Time values...

in C#
we dont have a Date DataType, we have only DateTime which is a combination of Date and Time
You can get the Date Value in string format only not the DateTime Object.

use SOlutoin 1 for Getting in string format.
and your code for actual DateTime.

Hope you understood.
come back if you need more clarification.


_________
Karthik


 
Share this answer
 
Try
dateForm = Convert.ToDateTime(dr["DateForm"]).Date;


Hope this helps,
Fredrik
 
Share this answer
 
Comments
Member 10548723 31-Jan-14 6:04am    
Didn't work.
Fredrik Bornander 31-Jan-14 6:09am    
What happened?
Member 10548723 31-Jan-14 6:12am    
Still is being retrieve as datetime i am not sure what is the error
Fredrik Bornander 31-Jan-14 6:14am    
What do you mean by Date then? Do you not want this as an instance of the type DateTime?
Member 10548723 31-Jan-14 7:05am    
For example 31/1/2014 not this 31/1/2014 12.00AM

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