strange result of Date value comparsion

Discussion in 'ASP General' started by c676228, Dec 9, 2008.

  1. c676228

    c676228 Guest

    Hi all,

    I hope I am not out of my mind.

    I have the following code:
    The iMonthlenght here is 1, EffectiveDate is 12/16/2008,
    expiration date is 1/12/2009. I displayed in the web already.

    '*****************************
    Response.Write "Effective Date Value again: " & DateAdd("m", iMonthLength,
    EffectiveDate) & "<br>"
    Response.Write "Expiration Date again before comparision: " &
    ExpirationDate & "<br>"
    '********************
    'after checking, the debugging message still shows that EffectiveDate is
    12/16/2008,
    expiration date is 1/12/2009, while DateAdd("m", iMonthLength,
    EffectiveDate) is
    1/16/2009. but somehow
    DateAdd("m", iMonthLength, EffectiveDate) <= ExpirationDate
    evaluates to be true. I don't get it. How come 1/16/2009 is earlier than
    1/12/2009.

    if DateAdd("m", iMonthLength, EffectiveDate) <= ExpirationDate then
    'my program gets to here for the data I gave
    above????
    Response.Write "strange value again?: " & "<br>"
    Response.Write DateAdd("m", iMonthLength, EffectiveDate)
    Response.Write "<br>"
    Response.Write ExpirationDate & "<br>"
    ' it tells me in black and while that my
    effectiveDate and expiration don't change at all, that's what I expected but
    how come this statement -DateAdd("m", iMonthLength, EffectiveDate) <=
    ExpirationDate
    would be true?

    end if

    --
    Betty
     
    c676228, Dec 9, 2008
    #1
    1. Advertising

  2. Hi Betty,

    My name is Allen Chen. It's my pleasure to work with you on this issue.

    From your description the problem is that the following expression returns
    true:

    DateAdd("m", iMonthLength, EffectiveDate) <= ExpirationDate

    when DateAdd("m", iMonthLength, EffectiveDate) is 1/16/2009 and
    ExpirationDate is 1/12/2009. Is my understanding correct?

    If so I think I need to know the type of the EffectiveDate and
    ExpirationDate. In addition, I think the code of DateAdd method is also
    important for us to diagnose this issue. So could you send me a demo
    project that can reproduce this problem? My email is
    . Please update here after sending the project in
    case I missed that email.

    Regards,
    Allen Chen
    Microsoft Online Community Support

    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    .

    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

    Note: MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 2 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions. Issues of this
    nature are best handled working with a dedicated Microsoft Support Engineer
    by contacting Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
    ==================================================
    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Allen Chen [MSFT], Dec 9, 2008
    #2
    1. Advertising

  3. "Allen Chen [MSFT]" <> wrote in message
    news:...
    > Hi Betty,
    >
    > My name is Allen Chen. It's my pleasure to work with you on this issue.
    >
    > From your description the problem is that the following expression returns
    > true:
    >
    > DateAdd("m", iMonthLength, EffectiveDate) <= ExpirationDate
    >
    > when DateAdd("m", iMonthLength, EffectiveDate) is 1/16/2009 and
    > ExpirationDate is 1/12/2009. Is my understanding correct?
    >
    > If so I think I need to know the type of the EffectiveDate and
    > ExpirationDate. In addition, I think the code of DateAdd method is also
    > important for us to diagnose this issue.


    In that case you best go dust off some old source code for VBScript, DateAdd
    is function intrinsic to VBScript.

    --
    Anthony Jones - MVP ASP/ASP.NET
     
    Anthony Jones, Dec 9, 2008
    #3
  4. "c676228" <> wrote in message
    news:...
    > Hi all,
    >
    > I hope I am not out of my mind.
    >
    > I have the following code:
    > The iMonthlenght here is 1, EffectiveDate is 12/16/2008,
    > expiration date is 1/12/2009. I displayed in the web already.
    >
    > '*****************************
    > Response.Write "Effective Date Value again: " & DateAdd("m", iMonthLength,
    > EffectiveDate) & "<br>"
    > Response.Write "Expiration Date again before comparision: " &
    > ExpirationDate & "<br>"
    > '********************
    > 'after checking, the debugging message still shows that EffectiveDate is
    > 12/16/2008,
    > expiration date is 1/12/2009, while DateAdd("m", iMonthLength,
    > EffectiveDate) is
    > 1/16/2009. but somehow
    > DateAdd("m", iMonthLength, EffectiveDate) <= ExpirationDate
    > evaluates to be true. I don't get it. How come 1/16/2009 is earlier than
    > 1/12/2009.
    >
    > if DateAdd("m", iMonthLength, EffectiveDate) <= ExpirationDate then
    > 'my program gets to here for the data I gave
    > above????
    > Response.Write "strange value again?: " & "<br>"
    > Response.Write DateAdd("m", iMonthLength, EffectiveDate)
    > Response.Write "<br>"
    > Response.Write ExpirationDate & "<br>"
    > ' it tells me in black and while that my
    > effectiveDate and expiration don't change at all, that's what I expected
    > but
    > how come this statement -DateAdd("m", iMonthLength, EffectiveDate) <=
    > ExpirationDate
    > would be true?
    >


    I don't think you are going out of your mind. Here is a one line VBS file
    which works fine:-

    MsgBox DateAdd("m" , 1, "12/16/2008") <= CDate("1/12/2009")

    However in this specific case this would also work:-

    MsgBox DateAdd("m" , 1, "12/16/2008") <= "1/12/2009"

    This would compare strings, however if infact ExpirationDate contains say
    some control characters like tab or CR at the start it wouldn't work. If
    you haven't done so already make sure the variables are actually dates using
    CDate. Also use Response.Write GetLocale() to discover what locale VBScript
    thinks its using. The tests above are done with "en-us" locale.

    --
    Anthony Jones - MVP ASP/ASP.NET
     
    Anthony Jones, Dec 9, 2008
    #4
  5. c676228 wrote on Mon, 8 Dec 2008 17:16:04 -0800:

    > Hi all,


    > I hope I am not out of my mind.


    > I have the following code:
    > The iMonthlenght here is 1, EffectiveDate is 12/16/2008, expiration
    > date is 1/12/2009. I displayed in the web already.


    > '*****************************
    > Response.Write "Effective Date Value again: " & DateAdd("m",
    > iMonthLength,
    > EffectiveDate) & "<br>"
    > Response.Write "Expiration Date again before comparision: " &
    > ExpirationDate & "<br>"
    > '********************
    > 'after checking, the debugging message still shows that EffectiveDate
    > is 12/16/2008, expiration date is 1/12/2009, while DateAdd("m",
    > iMonthLength,
    > EffectiveDate) is 1/16/2009. but somehow
    > DateAdd("m", iMonthLength, EffectiveDate) <= ExpirationDate evaluates
    > to be true. I don't get it. How come 1/16/2009 is earlier than
    > 1/12/2009.


    > if DateAdd("m", iMonthLength, EffectiveDate) <= ExpirationDate then
    > 'my program gets to here for the data I gave above????
    > Response.Write "strange value again?: " & "<br>"
    > Response.Write DateAdd("m", iMonthLength, EffectiveDate)
    > Response.Write "<br>"
    > Response.Write ExpirationDate & "<br>"
    > ' it tells me in black and while that
    > my effectiveDate and expiration don't change at all, that's what I
    > expected but how come this statement -DateAdd("m", iMonthLength,
    > EffectiveDate) <=
    > ExpirationDate would be true?


    > end if


    > --
    > Betty



    As well as Anthony's suggestion that you might be doing a string comparison
    (so just put CDate around the ExpirationDate in the If statement), have you
    checked the regional settings to make sure the date formats are US? It might
    be that 12/16/2008 is 16th Dec 2008 (because there is no month 16, so the
    script engine knows that it must be Dec 16th), but 1/12/2009 might be
    treated as 1st Dec 2009 (in UK time format) and so when you compare to your
    1 month added date 1/16/2009 which would be 16th Jan 2009 (again, no month
    16 so the script engine swaps the day and month around) then the comparison
    will be the opposite to what you expect.

    Can you add the following to your code before the comparison?

    Response.Write "EffectiveDate: " & FormatDateTime(EffectiveDate,vbLongDate)
    & "<br>"
    Response.Write "ExpirationDate: " &
    FormatDateTime(ExpirationDate,vbLongDate) & "<br>"

    and, assuming that the regional settings for long date format use the month
    name rather than number, it should help to determine if you're hitting a
    regional date handling issue as this will show the month names, eg. 12
    January 2009.

    --
    Dan
     
    Daniel Crichton, Dec 9, 2008
    #5
  6. c676228

    c676228 Guest

    Hi all,

    Thanks to you all for your guys input. As some of you imagined it does have
    something to do with date format.

    I figured out last night somehow. It is the format issue. I have to
    acknowledge that my husband(he is not a programmer) helped. He said it
    probably has something to do with date format.

    Initially I didn't take it seriously since I can DateDiff and DateAdd to both
    effectiveDate and expirationDate without any problem. But I checked my code
    both my effectiveDate and expirationDate are in string format before doing
    any calculation.
    I think it's probably like this: DateAdd and AddDiff etc. functions will
    return a Date formula even with string data type EffectiveDate, so in the
    following statement,
    the left side returns a Date format, while the right side is still in string
    format since it doesn't employ any date functions. When these two compare,
    the data is messed up.
    DateAdd("m", iMonthLength, EffectiveDate) <= ExpirationDate

    I fixed with EffectiveDate=CDate(EffectiveDAte)
    ExpirationDate=CDate(ExpirationDate)

    Then I can do comparisons and date calculations without any problems. The
    bug is really nasty since the behavior is not consistent, sometimes is Ok and
    sometimes is not OK. That's why it took me so long.

    Thanks for your contributions.

    Thank
    --
    Betty


    "Daniel Crichton" wrote:

    > c676228 wrote on Mon, 8 Dec 2008 17:16:04 -0800:
    >
    > > Hi all,

    >
    > > I hope I am not out of my mind.

    >
    > > I have the following code:
    > > The iMonthlenght here is 1, EffectiveDate is 12/16/2008, expiration
    > > date is 1/12/2009. I displayed in the web already.

    >
    > > '*****************************
    > > Response.Write "Effective Date Value again: " & DateAdd("m",
    > > iMonthLength,
    > > EffectiveDate) & "<br>"
    > > Response.Write "Expiration Date again before comparision: " &
    > > ExpirationDate & "<br>"
    > > '********************
    > > 'after checking, the debugging message still shows that EffectiveDate
    > > is 12/16/2008, expiration date is 1/12/2009, while DateAdd("m",
    > > iMonthLength,
    > > EffectiveDate) is 1/16/2009. but somehow
    > > DateAdd("m", iMonthLength, EffectiveDate) <= ExpirationDate evaluates
    > > to be true. I don't get it. How come 1/16/2009 is earlier than
    > > 1/12/2009.

    >
    > > if DateAdd("m", iMonthLength, EffectiveDate) <= ExpirationDate then
    > > 'my program gets to here for the data I gave above????
    > > Response.Write "strange value again?: " & "<br>"
    > > Response.Write DateAdd("m", iMonthLength, EffectiveDate)
    > > Response.Write "<br>"
    > > Response.Write ExpirationDate & "<br>"
    > > ' it tells me in black and while that
    > > my effectiveDate and expiration don't change at all, that's what I
    > > expected but how come this statement -DateAdd("m", iMonthLength,
    > > EffectiveDate) <=
    > > ExpirationDate would be true?

    >
    > > end if

    >
    > > --
    > > Betty

    >
    >
    > As well as Anthony's suggestion that you might be doing a string comparison
    > (so just put CDate around the ExpirationDate in the If statement), have you
    > checked the regional settings to make sure the date formats are US? It might
    > be that 12/16/2008 is 16th Dec 2008 (because there is no month 16, so the
    > script engine knows that it must be Dec 16th), but 1/12/2009 might be
    > treated as 1st Dec 2009 (in UK time format) and so when you compare to your
    > 1 month added date 1/16/2009 which would be 16th Jan 2009 (again, no month
    > 16 so the script engine swaps the day and month around) then the comparison
    > will be the opposite to what you expect.
    >
    > Can you add the following to your code before the comparison?
    >
    > Response.Write "EffectiveDate: " & FormatDateTime(EffectiveDate,vbLongDate)
    > & "<br>"
    > Response.Write "ExpirationDate: " &
    > FormatDateTime(ExpirationDate,vbLongDate) & "<br>"
    >
    > and, assuming that the regional settings for long date format use the month
    > name rather than number, it should help to determine if you're hitting a
    > regional date handling issue as this will show the month names, eg. 12
    > January 2009.
    >
    > --
    > Dan
    >
    >
    >
     
    c676228, Dec 10, 2008
    #6
  7. Gazing into my crystal ball I observed
    (Allen Chen [MSFT]) writing in
    news::

    > Regards,
    > Allen Chen
    > Microsoft Online Community Support
    >

    <snip 23 remaining line of signature>

    Allen, please, please, for the sake God and all humanity, reduce your
    signature to the Usenet conventional 4 lines,
    eg:

    -- (that is two dashes, a space and a carriage return)
    Allen Chen
    Microsoft Online Community Support
    Your URL (if you want to)

    See [http://en.wikipedia.org/wiki/Signature_block]. By using a
    conventional signature, when someone replies your signature will
    automatically be removed by compliant news clients. It will also make
    the rest of us a little more joyful.

    --
    Adrienne Boswell at Home
    Arbpen Web Site Design Services
    http://www.cavalcade-of-coding.info
    Please respond to the group so others can share
     
    Adrienne Boswell, Dec 13, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,285
    Michael Borgwardt
    May 30, 2004
  2. moonhk
    Replies:
    5
    Views:
    548
    moonhk
    Oct 11, 2006
  3. J.Ram
    Replies:
    7
    Views:
    655
  4. Michael Tan
    Replies:
    32
    Views:
    1,005
    Ara.T.Howard
    Jul 21, 2005
  5. Michael
    Replies:
    3
    Views:
    87
    Michael Yang
    May 11, 2007
Loading...

Share This Page