1

I have certain dates in my DB.

When received in C# Code, they come with values as 2012-02-14T06:02:33, 2014-09-12T03:21:22 and similar.

I want to truncate the time component to 00:00:00, ex: 2012-02-14T00:00:00.

How should I proceed with it?

Tried DateTime.TryPareExact with few formats but wasn't able to get the desired results.

My main aim is to just truncate the time part in the dates to 00:00:00.

Experts please help.

Anurag
  • 552
  • 9
  • 31
  • http://stackoverflow.com/questions/683037/how-to-compare-only-date-without-time-in-datetime-types-in-c – Mick Oct 30 '15 at 05:13

2 Answers2

2

There is no conversion in datetime. If you want to convert a string to datetime, use DateTime.TryParse(). If you have a datetime and want a formatted string. You can use a format string:

 dateAndTime.ToString("MMMM dd,yyyy");

will give you a formatted date string.

Ben
  • 2,433
  • 5
  • 39
  • 69
Jayanti Lal
  • 1,175
  • 6
  • 18
1

You can assign the value to a datetime object and can use the .Date property to get the date part only.

var dateValue = <<Get Value from DB>>;

var dateValue = dateValue.Date; // To get only the date part
user1672994
  • 10,509
  • 1
  • 19
  • 32