Another DateDiff() problem to add to the list

T

TofuTheGreat

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.
 
E

Evertjan.

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>"
%>
 
B

Bob Barrows [MVP]

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
 
T

TofuTheGreat

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).
 
E

Evertjan.

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

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
 
B

Bob Barrows [MVP]

TofuTheGreat said:
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
 
A

Anthony Jones

TofuTheGreat said:
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.
 
E

Evertjan.

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>"
 
T

TofuTheGreat

Again, probably you are wrong.


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

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.
 
E

Evertjan.

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

[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
BTW, I only mentioned the browser settings as I thought people might
question it. If you look back I mentioned the IIS configuration.

OK.
 
T

TofuTheGreat

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.
 
E

Evertjan.

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]
 
A

Anthony Jones

TofuTheGreat said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top