【问题标题】:check the given date is present between fromdate to todate检查给定日期是否存在于从日期到今天之间
【发布时间】:2012-02-25 22:07:41
【问题描述】:
我的数据集格式是这样的
EMPNAME FRMDATE TODATE
ANU 01-10-2012 01-20-2012
HARI 01-05-2012 02-05-2012
现在通过文本框获取特定员工的输入,例如 01-17-2012。
我的问题是:如何检查 i/p 日期是否在数据集中这两列 (FRMDATE,TODATE) 之间?
【问题讨论】:
标签:
c#
asp.net
dataset
datarowview
【解决方案1】:
试试这个
DataRow []_dr= ds.Tables[0].Select( inputDate +">= FRMDATE AND "+inputDate +" <= TODATE");
【解决方案2】:
我相信以下方法会对您有所帮助,有关比较日期的更多阅读材料,请查看以下两个主题:
Using linq or lambda to compare dates
Check if datetime instance falls in between other two datetime objects
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
public bool IsDateInRange(string date, string employeeId)
{
DateTime dateToCompare = DateTime.MinValue;
bool isInRange = false;
if (!String.IsNullOrEmpty(date) && !String.IsNullOrEmpty(employeeId) &&
DateTime.TryParse(date, out dateToCompare))
{
DataTable table = new DataTable();
string connectionString = WebConfigurationManager.ConnectionStrings["conn"].ConnectionString;
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand())
{
command.Connection = connection;
command.CommandText = "SELECT TOP 1 * FROM EmployeeDates WHERE EMPNAME = @EmpName";
command.Parameters.AddWithValue("@EmpName", employeeId);
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(table);
DateTime fomDate = (DateTime)table.Rows[0]["FRMDATE"];
DateTime toDate = (DateTime)table.Rows[0]["TODATE"];
//DateTime.Ticks converts a date into long
//Now you can simply compare whether the input date falls between the required range
if (dateToCompare.Ticks >= fomDate.Ticks && dateToCompare.Ticks <= toDate.Ticks)
{
isInRange = true;
}
connection.Close();
}
}
}
return isInRange;
}
【解决方案3】:
db.ClubPorsant.Where(p => p.CreateDate>= _FromDate && p.CreateDate p.MablaghVariz).ThenByDescending(p => p.Shomarehesab).ToList() ;