我希望从dateTime字段中删除时间戳。
int[] projects = {11157, 11138};
protected void compare(System.Data.DataTable dt1, System.Data.DataTable dt2, int[] arr)
{
differences.Columns.Add("ID", typeof(int));
differences.Columns.Add("BRS Date", typeof(string));
foreach (int value in arr)
{
DataRow[] result = dt1.Select("ChangeRequestNo = '" + arr +"' OR [GPP ID] = '" + arr + "'");
string brs = "";
string srs = "";
if (result.Length > 1)
{
DataRow row1 = result[0];
DataRow row2 = result[1];
DateTime row1date = (DateTime)row1[2];
DateTime row2date = (DateTime)row2[10];
//Remove the Time stamp on the dateTime
//variable coming from the GPP Extract
//row2date.cast(floor(cast(getdate() as float)) as datetime);
if (row1date.Equals(row2date))
{
// find your Image control in the Row
//Image image = ((Image)differences.Rows[1].FindControl("img"));
// set the path of image
//img.ImageUrl = "path of image";
brs = "True";
}
这个问题可能是通过SQLserver提出的,但对C#不起作用,除非我做错了(最有可能):
How can I truncate a datetime in SQL Server?
基本上我变量的值是“9/12/2008 12:00:00 AM”,我想要“9/12/2008”。我使用了一些演员操作符的变体,但没有运气。
CAST(BRSDate AS Date) -> Does nothing
CAST(BRSDate AS varchar(11)) -> 2009-05-26 and some dates come out as 0001-01-01
我为重新发帖道歉,但不幸的是,我找到的解决方案对我不起作用=(。有人可以帮忙吗?
答案 0 :(得分:5)
DateTime
包含TimeStamp
,您无法真正截断该部分,您可以使用Date
属性,其时间戳设置为12:00:00 AM
,或者您可以通过仅获取日期部分来格式化您的日期,然后进行比较。
所以为了比较你可以使用:
if (row1date.Date.Equals(row2date.Date))