Elmira Golshan
Elmira Golshan

Reputation: 57

I want to get only year/month/day in a correct format from my datatime type field in my table in SQL Server

I have table in SQL Server it has a orderDate column with datetime type

enter image description here

I use using System.Globalization in my asp.net page and use this code to get my system date history of solar format like this 1391/01/29 year/month/day:

 PersianCalendar pc=new PersianCalendar(); 
 DateTime dt = DateTime.Now; 
 string year= pc.GetYear(dt).ToString();
 string month= pc.GetMonth(dt).ToString();
 string day= pc.GetDayOfMonth(dt).ToString();

and it works fine and then I store the date in my table and then write a select query in SQL Server it works fine but when I get this column value and show it in my grid view which uses SqlDataSource in another asp.net page it shows the result like this :

AM 12:00:00  9/1/1391 

But it should show the result like

1391/09/01

I don't know why this happen?

Upvotes: 0

Views: 966

Answers (2)

revoua
revoua

Reputation: 2059

Or you can convert date value on server before passing to application.

SELECT replace(convert(varchar(10),getdate(),120),'-','/') date

Upvotes: 1

Glenn Stevens
Glenn Stevens

Reputation: 2008

Set the DataFormatString property on the bound field:

<asp:BoundField DataField="<Your Date Field>" HeaderText="<Column Header>"
                DataFormatString="{0:yyyy-MM-dd}"  />

Upvotes: 2

Related Questions