![]() |
strange result of Date value comparsion
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 |
RE: strange result of Date value comparsion
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 v-alchen@microsoft.com. 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: msdnmg@microsoft.com. ================================================== Get notification to my posts through email? Please refer to http://msdn.microsoft.com/en-us/subs...#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/subs.../aa948874.aspx ================================================== This posting is provided "AS IS" with no warranties, and confers no rights. |
Re: strange result of Date value comparsion
"Allen Chen [MSFT]" <v-alchen@online.microsoft.com> wrote in message news:OM6oWqcWJHA.2064@TK2MSFTNGHUB02.phx.gbl... > 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 |
Re: strange result of Date value comparsion
"c676228" <betty@newsgroup.nospam> wrote in message news:7D752646-38F0-4827-B721-17E283B8C6A0@microsoft.com... > 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 |
Re: strange result of Date value comparsion
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 |
Re: strange result of Date value comparsion
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 > > > |
RE: strange result of Date value comparsion
Gazing into my crystal ball I observed v-alchen@online.microsoft.com
(Allen Chen [MSFT]) writing in news:OM6oWqcWJHA.2064@TK2MSFTNGHUB02.phx.gbl: > 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 |
| All times are GMT. The time now is 10:37 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.