Click here to Skip to main content
14,925,595 members
Articles / Programming Languages / C#
Posted 9 Oct 2011

Tagged as


2 bookmarked

Partial DateTime Object Equality

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
9 Oct 2011CPOL
This is an alternative to "Partial DateTime Object Equality".

I don't mind multiple exits in such a simple method, hence I'd write:

public static bool Equals(this DateTime now, DateTime then, DatePartFlags flags) {
    if ((flags & DatePartFlags.Ticks) != 0 && now.Ticks != then.Ticks) return false;
    if ((flags & DatePartFlags.Month) != 0 && now.Month != then.Month) return false;
    if ((flags & DatePartFlags.Year) != 0 && now.Year != then.Year) return false;
    return true;

Some remarks:

  1. This offers short-circuiting.
  2. You might want to reorder the test statements if bigger DT parts are more relevant in your world.
  3. I changed the method name to conform to .NET conventions.
  4. I assume a non-zero DatePartFlags.Ticks value, which makes more sense to me.
  5. I object to the original FlagIsSet() method: it is confusing when the second parameter does not have exactly one bit set.


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


About the Author

Luc Pattyn
Software Developer (Senior)
Belgium Belgium
I am an engineer with a background in electronics, software and mathematics.

I develop technical software, both for embedded systems and for desktop equipment. This includes operating systems, communication software, local networks, image processing, machine control, automation, etc.

I have been using all kinds of microcontrollers and microprocessors (Intel 4004/8080/8051/80386/Pentium, Motorola 680x/680x0/ColdFire/PowerPC, Microchip PIC, Altera NIOS, and many more), lots of programming languages (all relevant assemblers, Fortran, Basic, C, Java, C#, and many more), and different operating systems (both proprietary and commercial).

For desktop applications and general development tools I have been using both UNIX systems and Mac/MacOS for many years, but I have switched to x86-based PCs with Windows, Visual Studio and the .NET Framework several years ago.

I specialize in:
- cross-platform development (making software that runs on diverse hardware/OS combinations)
- instruction set simulation
- improving software performance, i.e. making sure the software runs the job at hand in as short a time as possible on the given hardware. This entails algorithm selection, implementation design, accurate measurements, code optimisation, and sometimes implementing virtual machines, applying SIMD technology (such as MMX/SSE), and more.

Comments and Discussions

-- There are no messages in this forum --