views:

189

answers:

4

Hi. It seems I can't compare 2 date values with time.

When I compare just date part - year, month and day - everything works fine. But when I add comparing times everything breaks down and not even month fits.

So what's some basic algorithm for comparing 2 dates? I mean generally, I can't juse use dateTime1.CompareTo(dateTime2). Preferably in pseudocoude. Thanks.

Edit: Now I found out that it only compares with last comparision. So if last is day comparing it is compared by days and doesn't take into account year or month.

I just need basic algorithm for comparing 2 dates. I believe it must be simple...

+1  A: 

Compare the years. If one is bigger than the other, that's the later date.

Compare the months. If one is bigger than the other, that's the later date.

Compare the days. If one is bigger than the other, that's the later date.

Etcetera. I think the pattern is clear.

Pseudocode (and valid Python, incidentally):

if a.year > b.year:
  return 1
if a.year < b.year:
  return -1
if a.month > b.month:
  return 1
if a.month < b.month:
  return -1
# etcetera
return 0
Thomas
lol @ 'and valid Python, incidentally'
Fortega
+1  A: 

The following format should work pretty well:

DateTime a
DateTime b
if a.year != b.year
    return a.year < b.year
else if a.month != b.month
    return a.month < b.month
else if a.day != b.day
    return a.day < b.day
else if a.hour != b.hour
    return a.hour < b.hour
else if a.minute != b.minute
    return a.minute < b.minute
else
    // Dates are same
Aaron
What about localities? Say these dates were deserialized from logs all over the world, does this work?
Ariel
Yeah I would have take into account timezones, but that won't be necessary, because "the location" from where those dates come is just one with only one timezone :)
mnn
Before using this algorithm, each DateTime would need to be converted to the same locale if time zones were to play into your comparisons.
Aaron
A: 

I assume your problem is with localities. Assuming it's .NET, your dates contain localities, which are included in comparisons

dt1.CompareTo(dt2)

If you want to manually do it, then you should convert to ToUniversalTime() each and then compare.

Ariel
+1  A: 

This is decided NOT a language-neutral question. Java and Javasript and every SQL implementation I've ever used, for example, have date/time compare functions that work just fine. If the language or platform that you are using does not, you'd have to tell us what they are for anyone to be able to tell you what the correct function call to use is or how to work around some idiosyncracy. Yes, many languages have a date/time datatype, but that doesn't make the question language neutral. Most languages have a way to output text to the screen, but that doesn't make it legitimate to ask, "How do I write a text string to the screen in a language neutral way?" There is no answer to such a question.

I have no idea what you mean by "it only compares by last comparison". Again, you are apparently referring to the behavior of some particular comparison function in some particular language. You have to be more specific to get a meaningful answer.

Edit in reply to your comment: Different languages have different ways of storing dates. In Java, to see if one date/time is after another you write "date1.after(date2)" or "date1.compareTo(date2)>0". In Postgres SQL you write "date1>date2" or "date1::timestamp>date2::timestamp" depending on type definitions. In C there are a variety of "right" ways, including "difftime(date1, date2)". Etc etc.

Aaron's answer above is logically correct but irrelevant to how you actually compare dates in any language I use. If you had dates stored as a set of integers for each of these components (year, month, day, etc.), sure that would work. But I don't know of any language that stores dates that way.

If you have some reason for having your own format for storing dates, and breaking them out into components like this is useful for your application, then I suppose Aaron's technique is correct. But it is very unlikely that this is a useful format. Internally, Java stores date/time as number of milliseconds since midnight, Jan 1, 1970. C uses number of seconds. I'm sure other languages use other schemes.

You can, of course, write language-neutral pseudocode to compare two dates, just like you could write language-neutral pseudocode to describe writing to a screen. If your goal is to abstract the technical details of a language away while you work on a bigger problem, great. But if your goal is to actually compare two dates and find which is later or how many days are between them, then the answer lies in the technical details of how the particular language that you are using stores and manipulates dates.

Jay
Jesus... You're comparing apple to oranges. Comparing dates _is_ language neutral. Because in every language there's comparision operators, if statement, and return statement (or similar). On the other hand lanugages have different way to output something to screen, like cout<< or Console.WriteLine, or writeln, or printf().
mnn
@mnn: See edit above.
Jay
Still, I think that writing language neutral (pseudo)code to write to screen is much harder than to write comparing dates. But anyway Aaron answer didn't help as it was returning invalid values. So I'm gonna stick with your answer, which opened my eyes and I found "kind-of" unix timestamp functions for date and time in my language.
mnn