469,332 Members | 6,612 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,332 developers. It's quick & easy.

Javascript Daylight time problem...

Hi,

Well I am facing this strange issue with javascript. Specifically on
the 26th October 2003, when I select any roomtype I get the rate 0 if
"In the Operating system, Time Zone is selected with "Automatically
adjust clocks with daylight saving changes". why does this happen,
please have a look at this page:

http://autouae.bpweb.net/reservation.html
Please advise as I have not used any date specific functions at all...

-Dave
Jul 20 '05 #1
6 7776
da**********@hotmail.com (Dhaval) writes:
Well I am facing this strange issue with javascript. Specifically on
the 26th October 2003, when I select any roomtype I get the rate 0 if
I can see the problem. It happens in both IE and Opera.
If you chose the 25th of October instead, and chose two nights,
only one is counted, and likewise for any other selection that covers
the 26th. It is as if it isn't counted.

The problem is probably in the code (comments removed):
---
function dateAdd(baseStr,days){
baseTime = baseStr.split("/");
time = new Date(baseTime[2],baseTime[1]-1,baseTime[0]);

time = new Date(time.valueOf()+days*1000*3600*24);
return padDigit(time.getDate())+"/"+padDigit(time.getMonth()+1)+"/" +time.getYear();
}
---
which is called as:
---
departdate = dateAdd(arrivaldate,nights);
---
The problem is that the 26th of October is the day when daylight saving
changes back. That means that that day is 25 hours long. Your code
only multiply with 24, and adding 24 hours to the beginning of the 26th
is still on the 26th.

Code using 1000*3600*24 is usually not the smartest way to do what it
does (as I have been taught by reading this group :).

The best way to add a number of days to a date is:
---
time.setDate(time.getDate() + days);
---
That works by changing the date, not the millisecond. It overflows
correctly into next month, i.e., adding ten days to the 26th of
October gives the 5th of November.
http://autouae.bpweb.net/reservation.html Please advise as I have not used any date specific functions at all...


You use Date in several places. *Not* handling daylight saving is a very
date specific act two times a year.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
JRS: In article <r8**********@hotpop.com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <lr*@hotpop.com>
posted at Thu, 23 Oct 2003 14:41:13 :-

You use Date in several places. *Not* handling daylight saving is a very
date specific act two times a year.


More often than that. Outside the EU, many countries (some quite
respectable) change on different dates; some, such as Australia and New
Zealand, have good reason for doing so. I think the EU change dates,
surely the EU change-instants, are more commonly used than any others.

But at most twice per calendar year in any given location.

<FAQENTRY> The FAQ is rather terse with its date advice; it might help
if it had an explicit date-time question, and the one implicit in this
thread might be good to choose.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #3
Hi,

Well, I tried this code:

The best way to add a number of days to a date is:
---
time.setDate(time.getDate() + days);
---

but it doesn't serve the purpose. I do not intend to do date
calculations based on the user's system. I want to get the departday
based on the arrivaldate selected by the user and then adding the no
of nights he wants to book the room for. Is there any way we can
remove the 1000*3600*24 and add some other numbers which will take
care of the day light savings..or is there any other possible way that
we can achieve what I just mentioned above...

--------
function dateAdd(baseStr,days){
baseTime = baseStr.split("/");
time = new Date(baseTime[2],baseTime[1]-1,baseTime[0]);

time = new Date(time.valueOf()+days*1000*3600*24);
--------
-Dave
Jul 20 '05 #4
da**********@hotmail.com (Dhaval) writes:
Well, I tried this code:

The best way to add a number of days to a date is:
---
time.setDate(time.getDate() + days);
--- but it doesn't serve the purpose. I do not intend to do date
calculations based on the user's system.
Using the Date object will use the users timezone unless you change
it. Apart from that, I am not sure how the above line fails you. It
should do exactly what you want.
I want to get the departday based on the arrivaldate selected by the
user and then adding the no of nights he wants to book the room for.
That is what it does. It takes the "time" object created from the
arrivaldate, and adds (number of nights) days to it.
Is there any way we can remove the 1000*3600*24 and add some other
numbers which will take care of the day light savings..or is there
any other possible way that we can achieve what I just mentioned
above...


---
function dateAdd(baseStr,days){
baseTime = baseStr.split("/");
time = new Date(baseTime[2],baseTime[1]-1,baseTime[0]);

time.setDate(time.getDate() + days);
---

Try it!
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5
JRS: In article <bd**************************@posting.google.com >, seen
in news:comp.lang.javascript, Dhaval <da**********@hotmail.com> posted
at Thu, 23 Oct 2003 23:23:50 :-
Hi,

Well, I tried this code:

The best way to add a number of days to a date is:
---
time.setDate(time.getDate() + days);
---

but it doesn't serve the purpose. I do not intend to do date
calculations based on the user's system. I want to get the departday
based on the arrivaldate selected by the user and then adding the no
of nights he wants to book the room for. Is there any way we can
remove the 1000*3600*24 and add some other numbers which will take
care of the day light savings..or is there any other possible way that
we can achieve what I just mentioned above...
That gives the wrong answer sometimes; but it is probably the cleanest
way of doing what it actually does.
function dateAdd(baseStr,days){
baseTime = baseStr.split("/");
time = new Date(baseTime[2],baseTime[1]-1,baseTime[0]);

time = new Date(time.valueOf()+days*1000*3600*24);


function dateAdd(baseStr, days){
baseTime = baseStr.split("/")
time = new Date(baseTime[2], baseTime[1]-1, +baseTime[0]+days)

You should read ECMA-262 & consider whether it could be wise to put
parentheses round +baseTime[0] - and you should read up about date/time
in Javascript. The FAQ is, after all, there for a purpose.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #6
JRS: In article <is**********@hotpop.com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <lr*@hotpop.com>
posted at Fri, 24 Oct 2003 12:08:42 :-

Using the Date object will use the users timezone unless you change
it.


While that is certainly true, it is not necessarily important; the
sequence of Gregorian dates is independent of location.

Thus, provided that the UTC functions, including getTime setTime
valueOf, are not used, one can disregard the internal representation.

Conversely, use of 864e5 or equivalent is harmless if *only* UTC
functions are used.

After all, today is today, all fifty or thereabouts hours of it from its
appearance in the South Pacific until it vanishes in the Aleutians.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Bathroom_Monkey | last post: by
1 post views Thread by Drew | last post: by
3 posts views Thread by chrisdevey | last post: by
7 posts views Thread by Brett Edman | last post: by
1 post views Thread by maflatoun | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
reply views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.