-3

Hi I have this code in my .cs file and the output is 5/27/2017 12:00:00 AM but I want it to format only the date like 5/27/2017. This is my code to show the value in the label. If I add any parameter in the ToString("dd/MM/yyyy") it tells me No overload for method 'ToString' takes '1' arguments.

 Date.Text = ds.Tables[0].Rows[0]["AUM"].ToString();
Charles Xavier
  • 1,015
  • 3
  • 14
  • 33
  • is `ds.Tables[0].Rows[0]["AUM"]` actually a `DateTime` object? If so look at [ToShortTimeString](https://msdn.microsoft.com/en-us/library/system.datetime.toshorttimestring(v=vs.110).aspx) – Matt Burland May 18 '17 at 17:27

3 Answers3

2

If your data is DateTime:

Date.Text = ds.Tables[0].Rows[0]["AUM"].ToShortDateString();

If your data is a string:

Date.Text = Convert.ToDateTime(ds.Tables[0].Rows[0]["AUM"]).ToShortDateString();
ktyson
  • 84
  • 3
2

Cast to date time first

Date.Text = ((DateTime)ds.Tables[0].Rows[0]["AUM"]).ToString("dd/MM/yyyy");
aperezfals
  • 1,341
  • 1
  • 10
  • 26
  • Beautiful! It works!!! – Charles Xavier May 18 '17 at 17:29
  • 1
    Mark the answer as correct, please. Thanks! – aperezfals May 18 '17 at 17:30
  • There are better options as shown in http://stackoverflow.com/questions/7124434/display-only-date-and-no-time. This is indeed "one that helped me the most of half-answers to my post" and should be check-marked as "answer", but for real answer look at linked duplicate. – Alexei Levenkov May 18 '17 at 17:41
  • I agree it looks like the same question (the title) but the content in the answers were different and the answers in that post didn't help me. – Charles Xavier May 18 '17 at 17:51
  • If the sql data type of the field AUM is date time, then the Row have a C# DateTime in that column, but the value is a Object. You had to cast it to DateTime first. Then.. DateTime have a ToString("format") method – aperezfals May 18 '17 at 17:56
1

You need to look up these methods when you don't understand how their parameters work. https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx

That said, if you're pulling a DateTime value anyway, you can:

Date.Text = Convert.ToDateTime(ds.Tables[0].Rows[0]["AUM"]).ToShortDateString();

CDove
  • 1,940
  • 10
  • 19