1

This is my code by which i bind dropdownlist data from sql..i want to remove the time part from date...

string query = "select distinct PaperStartDate from HMDPaperManage ";
ddlPaperDate.DataSource = clsSqlFunctions.GetSelectedData(query);

ddlPaperDate.DataTextField = "PaperStartDate";
ddlPaperDate.DataBind();

23/04/2014 00:00:00:00

and i want

23/04/2014
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Marc
  • 97
  • 1
  • 7

5 Answers5

2
string query = "select distinct DATEADD(dd, DATEDIFF(dd, 0, PaperStartDate), 0) from HMDPaperManage "; 

try this solution

DATEADD(dd, DATEDIFF(dd, 0, PaperStartDate), 0) should take only date part

faby
  • 7,394
  • 3
  • 27
  • 44
2

You can replace the DropDownList values asbelow:

for(int i=0;i<ddlPaperDate.Items.Count;i++)
{
  ddlPaperDate.Items[i]=DateTime.ParseExact(ddlPaperDate.Items[i].Text,
      "dd/MM/yyyy HH:mm:ss",CultureInfo.InvariantCulture).ToString("dd/MM/yyyy");
}
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
1

Set the "DataTextFormatString to "d" i.e.

ddlPaperDate.DataTextFormatString = "d";

This gives the added benefit that you can change up how the date is formatted based on standard string.format() conversions.

For more info, see here

iamkrillin
  • 6,798
  • 1
  • 24
  • 51
  • 1
    The syntax is not correct. It should be ddlPaperDate.DataTextFormatString = "{0:d}" . Just putting "d" caused my dropdown textfield to populated with a "d". – Dave Mar 13 '17 at 12:59
1

You can just update your Query like

string query = "select distinct CONVERT(VARCHAR(12),PaperStartDate,103) as 'PaperStartDate' from HMDPaperManage ";
ddlPaperDate.DataSource = clsSqlFunctions.GetSelectedData(query);

ddlPaperDate.DataTextField = "PaperStartDate";
ddlPaperDate.DataBind();

In CONVERT(VARCHAR(12),PaperStartDate,103), 103 is the format code. You can have a lot of format codes. Below is the link for the other format codes: http://msdn.microsoft.com/en-us/library/ms187928.aspx

Usman Khalid
  • 3,032
  • 9
  • 41
  • 66
1

You should trim it in DB query

string query = "select distinct CONVERT(VARCHAR(10),PaperStartDate , 111) from HMDPaperManage ";

Check this for other formats

http://technet.microsoft.com/en-us/library/ms187928.aspx

Vijay Singh Rana
  • 1,060
  • 14
  • 32