如何在Asp.Net中从SQL Server数据库中获取当前日期的数据

本文关键字:数据库 获取 当前日期 数据 Server SQL Asp Net 中从 | 更新日期: 2023-09-27 17:58:28

我想根据当前日期从SQL Server数据库中获取数据。在数据库中,日期时间以2016-07-20 22:05:18.000 格式存储

我想要根据日期过滤器而不是时间的数据。

所以我做了这个:

string startDate = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
string endDate = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
DateTime startDate1 = DateTime.Parse(startDate);
DateTime endDate1 = DateTime.Parse(endDate);
private online_rechargesEntities db = new online_rechargesEntities();
ViewBag.billHistory =  db.tbl_userInfo.Where
(x => x.INSRT_TMSP >= StartDate1 && x.INSRT_TMSP <= EndDate1).ToList();

但它并没有返回正确的数据。

我做错了什么?

如何在Asp.Net中从SQL Server数据库中获取当前日期的数据

请确保startDate1、endDate1和SQL server数据库中的日期时间之间没有冲突。

最好尝试使用这些处理时区差异的新型DatetimeOffset。