Another DateDiff() problem to add to the list

Discussion in 'ASP General' started by TofuTheGreat@gmail.com, May 1, 2007.

  1. Guest

    I did a group search and found hundreds of cases of problems with
    DateDiff(). I read through several dozen but they didn not apply to
    my situation. Not having the time to read through all of them I'm
    hoping that I'll get an answer here.

    The software scenario. IIS 5.0 running on Windows Server 2000. The
    application is a campsite reservation system. The application starts
    with a simple HTML form that people enter an arrival date and a
    departure date. These two values are passed to the next page where I
    want to return the sites that are open between those two dates.
    Simple enough and DateDiff("d", ArrivalDate, DepartureDate) should
    return what I want.

    But it doesn't.

    If I enter May 2nd, 2007 into the form (i.e. 5/2/2007) as the arrival
    date and May 19th, 2007 (5/19/2007) as the departure date I don't get
    17 days as the returned value. I get a date as if I'd entered
    February 5th to May 19th and 103 days is the result returned.

    I've double-checked the regional date settings on the server and they
    are set for US (m/d/yyyy).

    Here's a snippet of the code that I'm using:

    Code:
    dim ArrivalDate, DepartureDate, NumNights
    
    ArrivalDate = request.form("txtArrivalDate")
    DepartureDate = request.form("txtDepartureDate")
    
    NumNights = Datediff("d", cdate(ArrivalDate),cdate(DepartureDate))
    
    Major PITA going on here.
    , May 1, 2007
    #1
    1. Advertising

  2. Evertjan. Guest

    wrote on 01 mei 2007 in microsoft.public.inetserver.asp.general:

    > I did a group search and found hundreds of cases of problems with
    > DateDiff(). I read through several dozen but they didn not apply to
    > my situation. Not having the time to read through all of them I'm
    > hoping that I'll get an answer here.
    >
    > The software scenario. IIS 5.0 running on Windows Server 2000. The
    > application is a campsite reservation system. The application starts
    > with a simple HTML form that people enter an arrival date and a
    > departure date. These two values are passed to the next page where I
    > want to return the sites that are open between those two dates.
    > Simple enough and DateDiff("d", ArrivalDate, DepartureDate) should
    > return what I want.
    >
    > But it doesn't.
    >
    > If I enter May 2nd, 2007 into the form (i.e. 5/2/2007) as the arrival
    > date and May 19th, 2007 (5/19/2007) as the departure date I don't get
    > 17 days as the returned value. I get a date as if I'd entered
    > February 5th to May 19th and 103 days is the result returned.
    >
    > I've double-checked the regional date settings on the server and they
    > are set for US (m/d/yyyy).


    So probably you are wrong there!!!!!!!!!!!

    > Here's a snippet of the code that I'm using:
    >
    >
    Code:
    >      dim ArrivalDate, DepartureDate, NumNights
    >
    >      ArrivalDate = request.form("txtArrivalDate")
    >      DepartureDate = request.form("txtDepartureDate")
    >
    >      NumNights = Datediff("d", cdate(ArrivalDate),cdate(DepartureDate))
    > 


    Testing/debugging will give you the answer:

    <%
    d1 = "5/2/2007"
    d2 = "5/19/2007"
    response.write "month(d1): " & month(d1) & "<br>"
    response.write "month(d2): " & month(d2) & "<br>"
    r = Datediff("d", cdate(d1),cdate(d2))
    response.write "difference in days: " & r & "<br>"
    %>



    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., May 1, 2007
    #2
    1. Advertising

  3. wrote:
    > I did a group search and found hundreds of cases of problems with
    > DateDiff(). I read through several dozen but they didn not apply to
    > my situation. Not having the time to read through all of them I'm
    > hoping that I'll get an answer here.
    >
    > The software scenario. IIS 5.0 running on Windows Server 2000. The
    > application is a campsite reservation system. The application starts
    > with a simple HTML form that people enter an arrival date and a
    > departure date. These two values are passed to the next page where I
    > want to return the sites that are open between those two dates.
    > Simple enough and DateDiff("d", ArrivalDate, DepartureDate) should
    > return what I want.
    >
    > But it doesn't.
    >
    > If I enter May 2nd, 2007 into the form (i.e. 5/2/2007) as the arrival
    > date and May 19th, 2007 (5/19/2007) as the departure date I don't get
    > 17 days as the returned value. I get a date as if I'd entered
    > February 5th to May 19th and 103 days is the result returned.
    >
    > I've double-checked the regional date settings on the server and they
    > are set for US (m/d/yyyy).
    >
    > Here's a snippet of the code that I'm using:
    >
    >
    Code:
    > dim ArrivalDate, DepartureDate, NumNights
    >
    > ArrivalDate = request.form("txtArrivalDate")
    > DepartureDate = request.form("txtDepartureDate")
    >
    > NumNights = Datediff("d", cdate(ArrivalDate),cdate(DepartureDate))
    > 
    >
    > Major PITA going on here.


    I hope you are entering your dates in a nonambiguous format. Try
    yyyy-mm-dd

    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
    Bob Barrows [MVP], May 1, 2007
    #3
  4. TofuTheGreat Guest

    On May 1, 12:55 pm, "Bob Barrows [MVP]" <>
    wrote:
    >
    > I hope you are entering your dates in a nonambiguous format. Try
    > yyyy-mm-dd
    >
    > --
    > Microsoft MVP -- ASP/ASP.NET
    > Please reply to the newsgroup. The email account listed in my From
    > header is my spam trap, so I don't check it very often. You will get a
    > quicker response by posting to the newsgroup.



    This campsite reservations system will be open to the public once it's
    done. The area I live in is the midwest USA so most everyone here is
    used to entering dates in mm/dd/yyyy format. The dates will get
    converted to datetime fields in the SQL database though. But for now
    I need to be able to allow people to enter dates in as mm/dd/yyyy, mm-
    dd-yyyy, etc.

    I've added a intermediary validation page that checks the date values
    before passing on to the next step. After doing this I noticed some
    weird things going on with the dates

    - Any date, in any month, that's less than 12 is getting the month and
    date switched around
    - For some reason using the MonthName() function returns month names
    in Finnish instead of English. o_O Why on earth would I get Finnish
    instead of English? I've checked the regional settings on the server,
    the PC I'm browsing/developing in, and the settings in the browsers
    (Firefox 2 and IE7).
    TofuTheGreat, May 2, 2007
    #4
  5. Evertjan. Guest

    TofuTheGreat wrote on 02 mei 2007 in
    microsoft.public.inetserver.asp.general:

    > On May 1, 12:55 pm, "Bob Barrows [MVP]" <>
    > wrote:
    >>
    >> I hope you are entering your dates in a nonambiguous format. Try
    >> yyyy-mm-dd
    >>


    Bob is right, see below.

    > This campsite reservations system will be open to the public once it's
    > done. The area I live in is the midwest USA so most everyone here is
    > used to entering dates in mm/dd/yyyy format. The dates will get
    > converted to datetime fields in the SQL database though. But for now
    > I need to be able to allow people to enter dates in as mm/dd/yyyy, mm-
    > dd-yyyy, etc.


    The way he input is written by the user
    should not influence way you should use the date:

    d1 = request.form("date1") ' "5/17/2007"
    d2 = split(d1,"/")
    d3 = d2(2) & "-" & d2(0) & "-" & d2(1) ' "2007-5-17"



    > I've added a intermediary validation page that checks the date values
    > before passing on to the next step. After doing this I noticed some
    > weird things going on with the dates
    >
    > - Any date, in any month, that's less than 12 is getting the month and
    > date switched around


    > - For some reason using the MonthName() function returns month names
    > in Finnish instead of English. o_O Why on earth would I get Finnish
    > instead of English? I've checked the regional settings on the server,


    Again, probably you are wrong.

    > the PC I'm browsing/developing in, and the settings in the browsers
    > (Firefox 2 and IE7).


    If this is about serverside asp-vbs code,
    it has nothing to do with browser settings


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., May 2, 2007
    #5
  6. TofuTheGreat wrote:
    > On May 1, 12:55 pm, "Bob Barrows [MVP]" <>
    > wrote:
    >>
    >> I hope you are entering your dates in a nonambiguous format. Try
    >> yyyy-mm-dd
    >>

    >
    > This campsite reservations system will be open to the public once it's
    > done. The area I live in is the midwest USA so most everyone here is
    > used to entering dates in mm/dd/yyyy format. The dates will get
    > converted to datetime fields in the SQL database though.


    Do you think this is going to happen by magic? What datetime value is
    the database supposed to convert '5/2/2007' to? You're thinking it's
    supposed to automagically know that this date is m/d/yyyy vs d/m/yyyy?

    > But for now
    > I need to be able to allow people to enter dates in as mm/dd/yyyy, mm-
    > dd-yyyy, etc.


    This will not work - believe me. Use a client-side calendar control or
    three dropdowns from which the user can select month, day and year.

    More reading you need to do:
    http://www.aspfaq.com/show.asp?id=2313 vbscript
    http://www.aspfaq.com/show.asp?id=2040 help with dates
    http://www.aspfaq.com/show.asp?id=2260 dd/mm/yyy confusion


    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
    Bob Barrows [MVP], May 2, 2007
    #6
  7. "TofuTheGreat" <> wrote in message
    news:...
    > On May 1, 12:55 pm, "Bob Barrows [MVP]" <>
    > wrote:
    > >
    > > I hope you are entering your dates in a nonambiguous format. Try
    > > yyyy-mm-dd
    > >
    > > --
    > > Microsoft MVP -- ASP/ASP.NET
    > > Please reply to the newsgroup. The email account listed in my From
    > > header is my spam trap, so I don't check it very often. You will get a
    > > quicker response by posting to the newsgroup.

    >
    >
    > This campsite reservations system will be open to the public once it's
    > done. The area I live in is the midwest USA so most everyone here is
    > used to entering dates in mm/dd/yyyy format. The dates will get
    > converted to datetime fields in the SQL database though. But for now
    > I need to be able to allow people to enter dates in as mm/dd/yyyy, mm-
    > dd-yyyy, etc.
    >
    > I've added a intermediary validation page that checks the date values
    > before passing on to the next step. After doing this I noticed some
    > weird things going on with the dates
    >
    > - Any date, in any month, that's less than 12 is getting the month and
    > date switched around
    > - For some reason using the MonthName() function returns month names
    > in Finnish instead of English. o_O Why on earth would I get Finnish
    > instead of English? I've checked the regional settings on the server,



    The most likely source of regional settings under which script runs are
    those of the DEFAULT user. These may not be the same as machine settings.
    In XP/2003 you can tick the Default user account settings check box in the
    regional options advanced tab. This will reset the DEFAULT user to use the
    current machine settings.

    Since most ASP runs under the anonymous user account (which doesn't usually
    have a user profile of its own) the settings for the default user will be
    used.

    NB. ASP caches regional settings from the profile of whatever user account
    happens to access the application first after a process re-start. Those
    settings then stick for all ASP processing regardless of the user account
    that it may run under. Therefore if you are using other forms of
    authentication which map to Domain or machine users which in turn have a
    profile on the server (because at some point they have logged on to it
    interactively) then potentially their regional settings have been cached and
    are in use by the server.



    > the PC I'm browsing/developing in, and the settings in the browsers
    > (Firefox 2 and IE7).
    >
    Anthony Jones, May 3, 2007
    #7
  8. Evertjan. Guest

    Anthony Jones wrote on 03 mei 2007 in
    microsoft.public.inetserver.asp.general:

    > The most likely source of regional settings under which script runs
    > are those of the DEFAULT user. These may not be the same as machine
    > settings. In XP/2003 you can tick the Default user account settings
    > check box in the regional options advanced tab. This will reset the
    > DEFAULT user to use the current machine settings.
    >
    > Since most ASP runs under the anonymous user account (which doesn't
    > usually have a user profile of its own) the settings for the default
    > user will be used.
    >
    > NB. ASP caches regional settings from the profile of whatever user
    > account happens to access the application first after a process
    > re-start. Those settings then stick for all ASP processing regardless
    > of the user account that it may run under. Therefore if you are using
    > other forms of authentication which map to Domain or machine users
    > which in turn have a profile on the server (because at some point they
    > have logged on to it interactively) then potentially their regional
    > settings have been cached and are in use by the server.
    >


    So the only reasonable advice about regional settings is:
    [speaking asp-vbs]

    DO NOT trust those settings.
    DO NOT trust those settings to remain as they are now.
    DO NOT trust string-to-date conversions to be stable.
    DO NOT use functions and defaults based on those settings.

    =======================================

    Make your own format functions using code and
    litterals like this:

    df = #2007-05-03#

    m="x/Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec"
    MonthStr = split(m,"/")

    ds = day(df)&" "&MonthStr(month(df))&" "&year(df)

    response.write ds & "<br>"


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., May 3, 2007
    #8
  9. TofuTheGreat Guest

    On May 2, 10:43 am, "Evertjan." <> wrote:
    > > I've added a intermediary validation page that checks the date values
    > > before passing on to the next step. After doing this I noticed some
    > > weird things going on with the dates

    >
    > > - Any date, in any month, that's less than 12 is getting the month and
    > > date switched around
    > > - For some reason using the MonthName() function returns month names
    > > in Finnish instead of English. o_O Why on earth would I get Finnish
    > > instead of English? I've checked the regional settings on the server,

    >
    > Again, probably you are wrong.
    >
    > > the PC I'm browsing/developing in, and the settings in the browsers
    > > (Firefox 2 and IE7).

    >
    > If this is about serverside asp-vbs code,
    > it has nothing to do with browser settings
    >
    > --
    > Evertjan.
    > The Netherlands.
    > (Please change the x'es to dots in my emailaddress)


    Sorry but I don't see how I can be wrong about the month name coming
    back in a different language? It really was coming back in a
    different language. I even took a screenshot to show someone else
    that didn't believe me. o_O

    BTW, I only mentioned the browser settings as I thought people might
    question it. If you look back I mentioned the IIS configuration.
    TofuTheGreat, May 4, 2007
    #9
  10. Evertjan. Guest

    TofuTheGreat wrote on 04 mei 2007 in
    microsoft.public.inetserver.asp.general:

    > On May 2, 10:43 am, "Evertjan." <> wrote:
    >> > I've added a intermediary validation page that checks the date
    >> > values before passing on to the next step. After doing this I
    >> > noticed some weird things going on with the dates

    >>
    >> > - Any date, in any month, that's less than 12 is getting the month
    >> > and date switched around
    >> > - For some reason using the MonthName() function returns month
    >> > names in Finnish instead of English. o_O Why on earth would I get
    >> > Finnish instead of English? I've checked the regional settings on
    >> > the server,

    >>
    >> Again, probably you are wrong.
    >>


    [please do not quote signatures]

    >
    > Sorry but I don't see how I can be wrong about the month name coming
    > back in a different language?


    I did not say that. I think you are wrong about the regional settings
    used by your IIS. It must be one of the two, don't you think?

    I showed you the solution by never depending on the IIS iregional
    settings.

    > It really was coming back in a
    > different language. I even took a screenshot to show someone else
    > that didn't believe me. o_O
    >
    >> > the PC I'm browsing/developing in, and the settings in the browsers
    >> > (Firefox 2 and IE7).

    >>
    >> If this is about serverside asp-vbs code,
    >> it has nothing to do with browser settings
    >>


    > BTW, I only mentioned the browser settings as I thought people might
    > question it. If you look back I mentioned the IIS configuration.


    OK.


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., May 4, 2007
    #10
  11. TofuTheGreat Guest

    Just wanted to follow-up on this in case others find the thread.

    I still have no idea what the problem was but the behavior disappeared
    after two reboots of the physical server. I'm suspecting a update
    conflict.

    And, for my sanity, I was NOT wrong about the regional settings on the
    server. I checked them four times. It would make sense that this
    would have been the problem but the settings were always correct when
    I went in to look.
    TofuTheGreat, May 14, 2007
    #11
  12. Evertjan. Guest

    TofuTheGreat wrote on 14 mei 2007 in
    microsoft.public.inetserver.asp.general:

    > Just wanted to follow-up on this in case others find the thread.


    What areyou talking about?

    [please always quote on usenet]

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., May 14, 2007
    #12
  13. "TofuTheGreat" <> wrote in message
    news:...
    > Just wanted to follow-up on this in case others find the thread.
    >
    > I still have no idea what the problem was but the behavior disappeared
    > after two reboots of the physical server. I'm suspecting a update
    > conflict.
    >
    > And, for my sanity, I was NOT wrong about the regional settings on the
    > server. I checked them four times. It would make sense that this
    > would have been the problem but the settings were always correct when
    > I went in to look.
    >


    Re-Read my NB in my previous response to your question. It is quite
    possible that after some future restart the problem will re-appear.
    Anthony Jones, May 17, 2007
    #13
    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. Mark Fox

    DateDiff in C#

    Mark Fox, Jul 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    12,894
    Kevin Spencer
    Jul 16, 2003
  2. DateDiff

    , Dec 29, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    466
    Jim in Arizona
    Dec 29, 2006
  3. CJM
    Replies:
    4
    Views:
    208
  4. Tim::.

    Datediff Problem

    Tim::., May 3, 2004, in forum: ASP General
    Replies:
    1
    Views:
    122
    Maarten
    May 3, 2004
  5. J-P-W

    DateDiff - Whole Month Problem

    J-P-W, Jul 14, 2006, in forum: ASP General
    Replies:
    4
    Views:
    163
    J-P-W
    Jul 17, 2006
Loading...

Share This Page