-3

I have to retrieve a date from the database in format YYYY-MM-DD and display it as the format:

Mon 25th

Currently my code looks like:

Label fixtureDateLabel = new Label();
fixtureDateLabel.Text = dReaderGameweekFixtures["fixtureDate"].ToString();

How do I adapt this to display in the format I want?

David
  • 15,894
  • 22
  • 55
  • 66
JackofAll
  • 517
  • 4
  • 13
  • 23
  • 2
    The `DATETIME` you're getting from the database (assuming SQL Server) doesn't have **any** string format - **you** define what the format should be, when calling `.ToString()` on it... – marc_s Mar 25 '13 at 15:57
  • 2
    Is your date stored in the DB as text, or as a `datetime` value? – Dai Mar 25 '13 at 15:58
  • Your question is not clear and is lacking some details.. please improve it. –  Mar 25 '13 at 15:58

4 Answers4

3

You can do this using the ToString method:

var result = DateTime.Parse(
  dReaderGameweekFixtures["fixtureDate"].ToString()
).ToString("ddd dd");

To my knowledge you'll need a little something extra to formulate the suffix to the day number, which is already conveniently provided here.

Community
  • 1
  • 1
Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
  • 1
    That'll only show "Mon 25" not "25th"... The "th" suffix is actually quite tricky. – Reed Copsey Mar 25 '13 at 15:58
  • @ReedCopsey Yes, and it's already been covered here on SO, I wasn't about to redo it, but had to find it. – Grant Thomas Mar 25 '13 at 15:59
  • 3
    Maybe you don't need the ToString/Parse to arrive at a DateTime, just a plain cast could be enough: `((DateTime)dReaderGameweekFixtures["fixtureDate"]).ToString("ddd dd")` – Hans Kesting Mar 25 '13 at 16:00
  • Adding "th" or any of the other suffixes seems tricky especially since those strings are not localized –  Mar 25 '13 at 16:16
1

You need to format the string according to one of the available formats:

http://msdn.microsoft.com/en-gb/library/az4se3k1.aspx

Pass it to ToString method.

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
0

Have go on the link below, would suffice your need and increase your knowledge...

http://www.dotnetperls.com/datetime-format

Glance
  • 108
  • 5
0

You should use ToString method of DateTime with format parameter

Check some info here - http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Label fixtureDateLabel = new Label();
DateTime d = dReaderGameweekFixtures["fixtureDate"];
fixtureDateLabel.Text = d.ToString("yyyy-MM-dd");
realnero
  • 994
  • 6
  • 12