ASP.NET上的日期格式,ASP.NET

时间:2017-02-03 04:43:45

标签: asp.net sql-server date gridview

我想在执行SELECT语句时仅从我的数据库返回日期部分。 (在下面的声明中:Task.ST_Date)

SELECT Task.Details, Task.Pr_ID, EmTask.T_ID, Task.Ref_ID, Task.ST_Date
FROM Task
INNER JOIN EmTask ON Task.Ref_ID = EmTask.T_ID
WHERE EmTask.Em_ID = 214432
    AND Task.STATUS = 'WAITING'
    AND Task.ST_Date BETWEEN CONVERT(VARCHAR(10), GETDATE(), 120)
        AND CONVERT(VARCHAR(10), GETDATE() + 6, 120)

此声明有效,但当我在gridview中查看时,此格式显示: 2/3/2017 12:00:00 AM enter image description here

有什么方法可以将格式修改为'yyyy-MM-dd'吗?

数据库(MSSQL)中的

*:date数据类型,yyyy-MM-dd格式 enter image description here

6 个答案:

答案 0 :(得分:0)

SELECT CONVERT(CHAR(10), convert(DATE, '2/3/2017 12:00:00 AM'), 126)

输出

DATE
2017-02-03

您可以自行更改查询

SELECT TASK.DETAILS, TASK.PR_ID, EMTASK.T_ID, TASK.REF_ID,CONVERT(CHAR(10), CONVERT(DATE, TASK.ST_DATE), 126)
FROM TASK
INNER JOIN EMTASK ON TASK.REF_ID = EMTASK.T_ID
WHERE EMTASK.EM_ID = 214432
    AND TASK.STATUS = 'WAITING'
    AND TASK.ST_DATE BETWEEN CONVERT(VARCHAR(10), GETDATE(), 120)
        AND CONVERT(VARCHAR(10), GETDATE() + 6, 120)

答案 1 :(得分:0)

使用

SELECT CONVERT(VARCHAR(10), Task.ST_Date, 120)

或在C#中Eval

Text='<%# Eval("ST_Date", "{0:yyyy-MM-dd}") %>'

<asp:boundfield datafield="ST_Date" dataformatstring="{0:yyyy-MM-dd}" />

答案 2 :(得分:0)

您可以格式化GridView列以所需格式显示日期

<asp:BoundField DataField="YourDateField" HeaderText="SomeHeader" 
                    DataFormatString="{0:yyyy-MM-dd}"  />

或者如果你有TemplateField

<asp:TemplateField HeaderText="Number">
    <ItemTemplate>
        <asp:Label ID="txtDate" runat="server" Text='<%# Convert.ToDateTime(DataBinder.Eval("dateTime")).ToString("yyyy-MM-dd") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

答案 3 :(得分:0)

您必须为DataType="System.DateTime"DataFormatString="{0:yyyy-MM-dd}"提供GridView自身的BoundField,如下所示。

<asp:BoundField DataType="System.DateTime" DataField="YourDateField" HeaderText="SomeHeader" DataFormatString="{0:yyyy-MM-dd}"  />

答案 4 :(得分:0)

根据所需格式格式化GridView列:

                                                                        '/&gt;                                                                                                                                      

答案 5 :(得分:0)

SELECT Task.Details, Task.Pr_ID, EmTask.T_ID, Task.Ref_ID,CONVERT(VARCHAR(10), Task.ST_Date , 120) as ST_Date
FROM Task
INNER JOIN EmTask ON Task.Ref_ID = EmTask.T_ID
WHERE EmTask.Em_ID = 214432
AND Task.STATUS = 'WAITING'
AND Task.ST_Date BETWEEN CONVERT(VARCHAR(10), GETDATE(), 120)
AND CONVERT(VARCHAR(10), GETDATE() + 6, 120)