基于当前日期的c#datetime值传递 [英] c# datetime value passing based on current date

查看:140
本文介绍了基于当前日期的c#datetime值传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从sql server的两个日期获取记录...
从表中选择*,其中@StartDate和@EndDate之间的CreatedDate 和i通过 5/12/2010 5/12/2010 (即)提取今天的记录...我有17个记录日期 5/12/2010 ,但没有一个似乎被选中....

I am trying to fetch records based on two dates from sql server... Select * from table where CreatedDate between @StartDate and @EndDate and i pass 5/12/2010 and 5/12/2010 (ie) fetching records for today... I have 17 records dated 5/12/2010 but none seems to get selected....

EDIT :
我使用这个,但是当我调试我的价值时,它显示 5/12/2010 12:00:00 AM

    DateTime baseDate = DateTime.Today;
    var today = baseDate;
    GetBookingReportByDate(today,today);

我在c#和可重用的存储过程中使用这些,需要 startdate lastdate 作为参数,

I am using these in c# and a resusable stored procedure which takes startdate and lastdate as parameters,

DateTime baseDate = DateTime.Today;

var today = baseDate;
var yesterday = baseDate.AddDays(-1);
var thisWeekStart = baseDate.AddDays(-(int)baseDate.DayOfWeek);
var thisWeekEnd = thisWeekStart.AddDays(7).AddSeconds(-1);
var lastWeekStart = thisWeekStart.AddDays(-7);
var lastWeekEnd = thisWeekStart.AddSeconds(-1);
var thisMonthStart = baseDate.AddDays(1 - baseDate.Day);
var thisMonthEnd = thisMonthStart.AddMonths(1).AddSeconds(-1);
var lastMonthStart = thisMonthStart.AddMonths(-1);
var lastMonthEnd = thisMonthStart.AddSeconds(-1);

我使用这些值,只能基于startdate和lastdate获取记录...正好像stackoverflow 今天,昨天,本周,上个月,本月,上个月 ....

I use these values and fetch records only based on startdate and lastdate... Exactly like stackoverflow Today,Yesterday,this week, last week,this month,Last month....

推荐答案

你没有包含时间部分...所以都被分析到相同的值。

You didn't include the time portion...so both are getting parsed to the same value.

你需要:

SELECT *
FROM Table
WHERE CreatedDate >= '5/12/2010 00:00:00'
    AND CreatedDate <= '5/12/200 23:59:59'

或: / p>

Or:

SELECT *
FROM Table
WHERE CreatedDate >= @StartDate
    AND CreatedDate <= DATEADD(day, 1, @StartDate)

更新

看到您的更新后,像第二个示例一样更改查询仍然可以正常工作。您也可以在C#代码中进行更改:

After seeing your update, changing the query like my second example would still work. You could also make the change in your C# code:

GetBookingReportByDate(today, today.AddDays(1));

这篇关于基于当前日期的c#datetime值传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆