Reputation: 137
i have date stored in a string format as follows: "2014-03-12" im passing this to a database which accepts the date as datetime. im converting the string date to datetime format as follows:
DateTime StartDates = Convert.ToDateTime(StartDate);
but the time gets appended along with the date as "2014-03-12 12:00:00:00"
can anyone tel me how to send only the date leaving out the time. i want the final date to be still in datetime format only but with time part cut off
Upvotes: 2
Views: 1125
Reputation: 10488
If you're happy with the date part, you may simply return the Date
property of the DateTime
conversion result:
DateTime StartDates = Convert.ToDateTime(StartDate).Date;
I should mention that using Convert.ToDateTime
puts you at the mercy of the process current culture date format though, so you probably want to use the ParseExact
or ToString
method like the other answers suggests, with the appropriate format and culture instance.
Upvotes: 0
Reputation: 223402
DateTime
is irrespective of the format. Formatting is only useful for presentation purpose. A DateTime
object will have a Date
part and Time
part. When you try parsing your string "2014-03-12"
, it doesn't have a Time
part, so in the parsed object, Time is set to 00:00:00
.
If you just want to to display date then you can use DateTime.ToShortDateString
method or use a custom format like:
string formattedDateString = StartDates.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
Upvotes: 5