Click here to Skip to main content
11,433,170 members (62,224 online)
Click here to Skip to main content

Tagged as

Partial DateTime Object Equality

, 19 Jan 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Determine if specified date/time properrties are equal
While working on a future article, I decided I need to be able to compare two DateTime objects based on any property I chose. In other words, if I wanted to see if two DateTimes objects had the same hour and minute, but didn't care about the rest of the values (year, month, etc) I would need to code it myself. To accomplish this, I wrote a couple of extension methods to satisfy the requirement.

[Flags]
public enum DatePartFlags {Ticks=0, Year=1, Month=2, Day=4, Hour=8, Minute=16, Second=32, Millisecond=64 };
public static class ExtensionMethods
{
    private static bool FlagIsSet(DatePartFlags flags, DatePartFlags flag)
    {
        bool isSet = ((flags & flag) == flag);
        return isSet;
    }
 
    public static bool Equal(this DateTime now, DateTime then, DatePartFlags flags)
    {
        bool isEqual = false;
        if (flags == DatePartFlags.Ticks)
        {
            isEqual = (now == then);
        }
        else
        {
            DatePartFlags equalFlags = DatePartFlags.Ticks;
            equalFlags |= (FlagIsSet(flags, DatePartFlags.Year) && now.Year == then.Year) ? DatePartFlags.Year : 0;
            equalFlags |= (FlagIsSet(flags, DatePartFlags.Month) && now.Month == then.Month) ? DatePartFlags.Month : 0;
            equalFlags |= (FlagIsSet(flags, DatePartFlags.Day) && now.Day == then.Day) ? DatePartFlags.Day : 0;
            equalFlags |= (FlagIsSet(flags, DatePartFlags.Hour) && now.Hour == then.Hour) ? DatePartFlags.Hour : 0;
            equalFlags |= (FlagIsSet(flags, DatePartFlags.Minute) && now.Minute == then.Minute) ? DatePartFlags.Minute : 0;
            equalFlags |= (FlagIsSet(flags, DatePartFlags.Second) && now.Second == then.Second) ? DatePartFlags.Second : 0;
            equalFlags |= (FlagIsSet(flags, DatePartFlags.Millisecond) && now.Millisecond == then.Millisecond) ? DatePartFlags.Millisecond : 0;
            isEqual = (flags == equalFlags);
        }
        return isEqual;
    }
}

Usage is as follows:

private void LoopUntilItHurts()
{
    DatePartFlags equalityFlags = DatePartFlags.Minute | DatePartFlags.Second;
    DateTime now = DateTime.Now;
    DateTime then = DateTime.Now;
    bool waiting = false;
 
    while (true)
    {
        now = DateTime.Now;
        if (!waiting)
        {
            int difference = now.Minute % 5;
            then = now.Add(new TimeSpan(0, 0, 1, 0, 0));
            waiting = true;
        }
 
        if (now.Equal(then, equalityFlags))
        {
            waiting = false;
            // it's time to do something important    
        }
        else
        {
            // sit and spin
            Thread.Sleep(1000);
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

John Simmons / outlaw programmer
Software Developer (Senior)
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
GeneralRe: terinary operations such as (true ? 0 : 1) are basically nes... Pin
jfriedman13-Jan-12 12:53
memberjfriedman13-Jan-12 12:53 
GeneralReason for my vote of 3 I would have used Enum.HasFlag and I... Pin
jfriedman12-Jan-12 15:39
memberjfriedman12-Jan-12 15:39 
GeneralRe: WHERE do you see a nested if statement? Use "goto"? That's ... Pin
John Simmons / outlaw programmer13-Jan-12 12:17
mvpJohn Simmons / outlaw programmer13-Jan-12 12:17 
GeneralReason for my vote of 4 Quite elegant Pin
dmjm-h24-Jan-11 11:34
memberdmjm-h24-Jan-11 11:34 
GeneralReason for my vote of 5 Very useful tip! Pin
Nuri Ismail19-Jan-11 22:47
memberNuri Ismail19-Jan-11 22:47 
GeneralReason for my vote of 5 Good and very useful. Will definitel... Pin
JF201519-Jan-11 21:38
memberJF201519-Jan-11 21:38 
GeneralReason for my vote of 5 Very nice. JSOP wrote: // sit and sp... Pin
thatraja19-Jan-11 15:21
mvpthatraja19-Jan-11 15:21 
GeneralRe: Had the same thought too :D Would give you a 5 for that com... Pin
JF201519-Jan-11 21:39
memberJF201519-Jan-11 21:39 
GeneralReason for my vote of 5 Nice one. Thanks for sharing. Pin
linuxjr19-Jan-11 11:05
memberlinuxjr19-Jan-11 11:05 
GeneralReason for my vote of 5 Useful! I like that. Pin
Manfred R. Bihy19-Jan-11 10:32
mvpManfred R. Bihy19-Jan-11 10:32 
Generald'accord! 5+ Pin
Manfred R. Bihy19-Jan-11 10:32
mvpManfred R. Bihy19-Jan-11 10:32 
GeneralReason for my vote of 5 5. Very nice. I may need to hijack ... Pin
Marcus Kramer19-Jan-11 9:14
memberMarcus Kramer19-Jan-11 9:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150428.2 | Last Updated 19 Jan 2011
Article Copyright 2011 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid