1

I am using c#. I have a problem i am unable to remove the time format from datetime variable . Here is my code.

 string Date = ds.Tables[0].Rows[0]["serviceDate"].ToString();
string ServiceDate = string.Format("{0:MM/dd/yyyy}", Date);

How to remove the time from this format?

X3Vikas
  • 101
  • 2
  • 9
  • I assume the column "serviceDate" is already a DateTime-object, why do you want to convert it to string? Just use it as DateTime as it is. – Esko Jul 06 '18 at 06:52
  • 3
    Wait, are you talking about the _content_ of the DateTime variable, or the _display_ of it? Do you want to throw away the time component, or just not show it on the screen? – Ryan Lundy Jul 06 '18 at 06:54
  • You can't remove the "Time Format" from a `DateTime`. A `DateTime` doesn't have a format. You can only remove the "Time" from a `DateTime` with a call to the `.Date` property. – Enigmativity Jul 06 '18 at 06:58
  • You really need to provide a [mcve] to get a good answer. – Enigmativity Jul 06 '18 at 07:02

2 Answers2

1

Call the Date property on your DateTime.

It gives you a DateTime with the same date, but with the time set to midnight.

Ryan Lundy
  • 204,559
  • 37
  • 180
  • 211
0

By converting the Date into DateTime it will work fine.

 DateTime date = Convert.ToDateTime(ds.Tables[0].Rows[0]["serviceDate"]);
 string serviceDate = string.Format("{0:MM/dd/yyyy}", date);

Or if your serviceDate Field is already a Datetime then you can try this.

string ServiceDate = string.Format("{0:MM/dd/yyyy}", ds.Tables[0].Rows[0]["serviceDate"]); 
shami sheikh
  • 552
  • 3
  • 13
  • 26