C# SqlDataSource中的日期时间转换
我目前正在编写一份报告,其中使用太平洋标准时间(PST)作为日期的基础 我注意到,有时在检索特定日期的数据时,会缺少列 我发现数据库时间/服务器时间使用UTC,而报表使用PST/Localtime 在代码的“代码隐藏”部分中,如果未选择任何选项,则这是他们使用的默认日期C# SqlDataSource中的日期时间转换,c#,asp.net,sql-server,utc,C#,Asp.net,Sql Server,Utc,我目前正在编写一份报告,其中使用太平洋标准时间(PST)作为日期的基础 我注意到,有时在检索特定日期的数据时,会缺少列 我发现数据库时间/服务器时间使用UTC,而报表使用PST/Localtime 在代码的“代码隐藏”部分中,如果未选择任何选项,则这是他们使用的默认日期 if (!Page.IsPostBack) { FromTB.Text = DateTime.Today.ToLocalTime().ToString("dddd, MMMM dd, yyyy h
if (!Page.IsPostBack)
{
FromTB.Text = DateTime.Today.ToLocalTime().ToString("dddd, MMMM dd, yyyy h:mm tt");
ToTB.Text = DateTime.Today.ToLocalTime().AddMinutes(1439).ToString("dddd, MMMM dd, yyyy h:mm tt");
}
下面是前面用于检索数据库中数据的代码
<asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:xxxxx %>' SelectCommand="select StudYear ,count(CAST(CreatedDate as DATE)) AS StudCount
from Student
where CreatedDate >= @CreatedDate and CreatedDate <= @CreatedDate2 and StudYear is not null
group by StudYear
order by StudYear"
<SelectParameters>
<asp:ControlParameter ControlID="FromTB" Name="CreatedDate" PropertyName="Text" Type="DateTime" />
<asp:ControlParameter ControlID="ToTB" Name="CreatedDate2" PropertyName="Text" Type="DateTime" />
</SelectParameters>
</asp:SqlDataSource>
顺便说一句,如果您想要某一天的所有结果,可能最好使用如下内容:
date>=2020-03-09T00:00:00Z&&date<2020-03-10T00:00:00Z
(即大于或等于今天开始,小于明天开始)而不是你目前的方法,它不会包含任何超出2020-03-09T23:59:00
。明白了。谢谢你的反馈,john。