English 中文(简体)
日期或时间
原标题:DateTime group by date or hours
  • 时间:2010-03-09 10:35:55
  •  标签:
  • c#
  • .net

2005年2月1日

01.02.2010 0:00:00 -> 01.02.2010 页: 1

因此,我的日期是:

 DateTime x;

it s 01.02.2010 0:00:00 as a string

 x.Date.ToString()

我在此比较了日期。

DatarowsForOneDay = dt.Select("DailyRecTime=  " + x.ToString() + " ");

因此,我如何按日加几个小时,不关心几分钟和秒钟。

最佳回答

你可以撰写自己的<条码>IEqualityComparer<Datetime>,仅比较你所关心的那部分时间。 LINQ 团体 载重:<代码> IEqualityComparer 。 我最近也有同样的问题,只是这样做了。

但是,在转换成星之前,你必须打上<条码>。 如果你可以的话,你可能会想建立<条码>。 缩略语

我现在没有与我一道制定原来的法典。 我从记忆中重新归类,没有测试。


public class DateAndHourComparer : IEqualityComparer
{
    public bool Equals(DateTime x, DateTime y)
    {
        var xAsDateAndHours = AsDateHoursAndMinutes(x);
        var yAsDateAndHours = AsDateHoursAndMinutes(y);

        return xAsDateAndHours.Equals(yAsDateAndHours);
    }

    private DateTime AsDateHoursAndMinutes(DateTime dateTime)
    {
        return new DateTime(dateTime.Year, dateTime.Month, 
                            dateTime.Day, dateTime.Hour, 
                            dateTime.Minute, 0);
    }

    public int GetHashCode(DateTime obj)
    {
        return AsDateHoursAndMinutes(obj).GetHashCode();
    }
}

我从来就没有这样作,但可以使用以上<代码>Datetime的代号,看像......。


public class DateAndHourStringComparer : IEqualityComparer
{
    private readonly DateAndHourComparer dateComparer = new DateAndHourComparer();

    public bool Equals(string x, string y)
    {
        var xDate = DateTime.Parse(x);
        var yDate = DateTime.Parse(y);

        return dateComparer.Equals(xDate, yDate);
    }

    public int GetHashCode(string obj)
    {
        var date = DateTime.Parse(obj);
        return dateComparer.GetHashCode(date);
    }
}

我没有对此进行测试,我没有增加检查或格式检查。 该守则旨在表明一般的想法。

问题回答

You can pass a parameter with DateTime.ToString(string pattern). More information @ http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签