Announcement

Collapse
No announcement yet.

How do I compare 2 dates?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How do I compare 2 dates?

    I have not been able to correctly compare 2 dates to determine if one is less than (occurs prior to) the other.
    For example:
    if(date1 < date2)
    {
    ...
    ...
    ...
    }

    Please tell me how to do it. I have unsuccessfully spent a lot of time on this. Seems like it should be easy. I probably will be quite embarrassed when I get the solution.

  • #2
    Hi jcm21,

    There are several ways to do this depending on how you are arriving at the date. One way is to use the millisecond value available from the efs engine or time object. Another way is to simply use a numerical representation of the date, and that is what I will show you.

    date1 = yyyymmdd ==>> you want to create a number in this format

    To do this, perform the following multiplication in two steps

    step1=(4 digit year)*100+month;
    step2=step1*100+day;

    By multiplying by 100, you ensure the single digit months and single digit days will be left padded with a zero. Today is June 7th 2008. The month is 6 and the day is 7. Using this technique would result in this numerical representation.

    20080607

    When you have both dates represented in this manner, an older date will be a larger number and your conditional will work fine.

    Hope this helps.

    Comment


    • #3
      Exactly what I needed, Steve. Many thanks.

      Exactly what I needed, Steve. Once again you have saved the day. Many Thanks.

      Joe Miller

      Comment


      • #4
        Hi Joe,

        You're welcome, good to hear that helped.

        Comment

        Working...
        X