为asp添加小时:boundfield日期控件

时间:2016-01-26 00:47:38

标签: asp.net

我需要修复一些我继承的遗留ASP.NET代码(ASP.NET 2.0),它使用<asp:BoundField>DateTime绑定到字段,就像这样;

<asp:BoundField DataField="Date" 
 DataFormatString="{0:T}" HeaderText="Time" HtmlEncode="False"
 SortExpression="Date" 
/>

问题是我需要在日期输出中添加小时数,因为它以UTC格式存储。

它从一个非常不稳定的<asp:SqlDataSource>获取数据 - 我会在这里添加,我不太了解SQL,而且我知道这些控件更少......

<asp:SqlDataSource ID="storeSessions" runat="server" ConnectionString="<%$ ConnectionStrings:MSSQL %>"
        ProviderName="<%$ ConnectionStrings:MSSQL.ProviderName %>" SelectCommand="SELECT Employees.FirstName, Employees.LastName, Logins.Date, Locations.Name AS Site FROM Logins INNER JOIN Employees ON Logins.Employee = Employees.Id INNER JOIN Locations ON Logins.Location = Locations.Id WHERE (Logins.Date BETWEEN @Date AND @Date + ' 23:59:59:997') AND (Logins.Location = @Site)">
        <SelectParameters>
            <asp:ControlParameter ControlID="insertBirthdayPicker" Name="Date" PropertyName="SelectedDate" DefaultValue="8/1/2006" />
            <asp:SessionParameter DefaultValue="0" Name="Site" SessionField="Site" />
        </SelectParameters>
    </asp:SqlDataSource>

有没有办法增加时间?

1 个答案:

答案 0 :(得分:1)

您可以按如下方式更改sql-query:

    SELECT Employees.FirstName, 
           Employees.LastName, 
           DATEADD(hour, -6, Logins.Date) as Date, --here you add (-6) to your datetime
           Locations.Name AS Site 
      FROM Logins 
INNER JOIN Employees 
        ON Logins.Employee = Employees.Id 
INNER JOIN Locations 
        ON Logins.Location = Locations.Id 
     WHERE (Logins.Date BETWEEN @Date AND @Date + ' 23:59:59:997') 
       AND (Logins.Location = @Site)