Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

In my database I have column of datatype date.

from c# I am doing this , which contains time as well ..
C#
DateTime date = new DateTime(fYear, month, 1)


any way which will removes time and store only dat to sql db ?

What I have tried:

DateTime date = new DateTime(fYear, month, 1).Date;
Posted
Updated 3-Mar-16 16:59pm
Comments
Patrice T 3-Mar-16 20:10pm    
Why is it a problem to use a DateTime type ?
[no name] 3-Mar-16 20:20pm    
As ppolymorphe is implying this is not such a good idea because it is error prone and removes the built in usefulness of DateTime. Better to learn how to use DateTime fully.
koolprasad2003 3-Mar-16 23:10pm    
.ToString() method will always helpful to you, just pass data format as paramerter,
e.g. DateTime.Now.ToString("M/d/yyyy");
Hope it helps

Depending on what type of Database you are using you will need to convert the DateTime in C# to a string.
Typically Date values are surrounded by #'s as below;
SQL
#1/1/2000#

The easiest way is to therefore convert the date to a string using
C#
DateTime.ToString("d/M/yyyy");

Take a look at the following MSDN articles;

MSDN - DateTime.ToString(string)[^]

MSDN - Standard Date and TIme Format Strings[^]

MSDN - Custom Date and Time Format Strings[^]
 
Share this answer
 
v2
C#
DateTime db = new DateTime(2016,12,3);

Console.WriteLine(db.ToString("yyyy-MM-dd"));

result: 2016-12-03
 
Share this answer
 

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