-1

I'm trying to retrieve the date from my database which has datetime datatype. How can I retrieve the date only? My code is currently like this:

startDateTb.Text = dtbl.Rows[0]["StartDate"].ToString();
Shreyas Pednekar
  • 1,285
  • 5
  • 31
  • 53
Jia Hao
  • 29
  • 5

3 Answers3

1

You can use string.Format("{0:MM/dd/yyyy}", DateTime.Parse(dtbl.Rows[0]["StartDate"].ToString()))

You can refer many format: // create date time 2008-03-09 16:05:07.123

DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);

String.Format("{0:y yy yyy yyyy}",      dt);  // "8 08 008 2008"   year
String.Format("{0:M MM MMM MMMM}",      dt);  // "3 03 Mar March"  month
String.Format("{0:d dd ddd dddd}",      dt);  // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}",          dt);  // "4 04 16 16"      hour 12/24
String.Format("{0:m mm}",               dt);  // "5 05"            minute
String.Format("{0:s ss}",               dt);  // "7 07"            second
String.Format("{0:f ff fff ffff}",      dt);  // "1 12 123 1230"   sec.fraction
String.Format("{0:F FF FFF FFFF}",      dt);  // "1 12 123 123"    without zeroes
String.Format("{0:t tt}",               dt);  // "P PM"            A.M. or P.M.
String.Format("{0:z zz zzz}",           dt);  // "-6 -06 -06:00"   time zone

// month/day numbers without/with leading zeroes
String.Format("{0:M/d/yyyy}",           dt);  // "3/9/2008"
String.Format("{0:MM/dd/yyyy}",         dt);  // "03/09/2008"

// day/month names
String.Format("{0:ddd, MMM d, yyyy}",   dt);  // "Sun, Mar 9, 2008"
String.Format("{0:dddd, MMMM d, yyyy}", dt);  // "Sunday, March 9, 2008"

// two/four digit year
String.Format("{0:MM/dd/yy}",           dt);  // "03/09/08"
String.Format("{0:MM/dd/yyyy}",         dt);  // "03/09/2008"

C# DateTime to "YYYYMMDDHHMMSS" format

Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
0

Try something like this

startDateTb.Text = dtbl.Rows[0]["StartDate"] is DBNull ? "" : Convert.ToDateTime(dtbl.Rows[0]["StartDate"]).ToString("dd/MM/yyyy");

check if column is null or not, then get formatted date from the column

Shreyas Pednekar
  • 1,285
  • 5
  • 31
  • 53
0

You can do two things. In your SQL Server side, do this:

/* date is in mm/dd/yyyy format */
SELECT CONVERT(VARCHAR(10),GETDATE(),101); 

or on c# side:

startDateTb.Text = Convert.toDateTime(dtbl.Rows[0]["StartDate"]).ToString("d/MM/yyyy");
Gauravsa
  • 6,330
  • 2
  • 21
  • 30