Javascript Daylight time problem...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dhaval

    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 "Automatica lly
    adjust clocks with daylight saving changes". why does this happen,
    please have a look at this page:




    Please advise as I have not used any date specific functions at all...

    -Dave
  • Lasse Reichstein Nielsen

    #2
    Re: Javascript Daylight time problem...

    da_linuxguru@ho tmail.com (Dhaval) writes:
    [color=blue]
    > 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[/color]

    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.value Of()+days*1000* 3600*24);
    return padDigit(time.g etDate())+"/"+padDigit(time .getMonth()+1)+ "/" +time.getYear() ;
    }
    ---
    which is called as:
    ---
    departdate = dateAdd(arrival date,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(ti me.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.
    [color=blue]
    > http://autouae.bpweb.net/reservation.html[/color]
    [color=blue]
    > Please advise as I have not used any date specific functions at all...[/color]

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

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Dr John Stockton

      #3
      Re: Javascript Daylight time problem...

      JRS: In article <r8142kk6.fsf@h otpop.com>, seen in
      news:comp.lang. javascript, Lasse Reichstein Nielsen <lrn@hotpop.com >
      posted at Thu, 23 Oct 2003 14:41:13 :-[color=blue]
      >
      >You use Date in several places. *Not* handling daylight saving is a very
      >date specific act two times a year.[/color]

      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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

      Comment

      • Dhaval

        #4
        Re: Javascript Daylight time problem...

        Hi,

        Well, I tried this code:

        The best way to add a number of days to a date is:
        ---
        time.setDate(ti me.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.value Of()+days*1000* 3600*24);
        --------


        -Dave

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: Javascript Daylight time problem...

          da_linuxguru@ho tmail.com (Dhaval) writes:
          [color=blue]
          > Well, I tried this code:
          >
          > The best way to add a number of days to a date is:
          > ---
          > time.setDate(ti me.getDate() + days);
          > ---[/color]
          [color=blue]
          > but it doesn't serve the purpose. I do not intend to do date
          > calculations based on the user's system.[/color]

          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.
          [color=blue]
          > 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.[/color]

          That is what it does. It takes the "time" object created from the
          arrivaldate, and adds (number of nights) days to it.
          [color=blue]
          > 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...[/color]

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

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

          Try it!
          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Dr John Stockton

            #6
            Re: Javascript Daylight time problem...

            JRS: In article <bd8dacba.03102 32223.6b8354f0@ posting.google. com>, seen
            in news:comp.lang. javascript, Dhaval <da_linuxguru@h otmail.com> posted
            at Thu, 23 Oct 2003 23:23:50 :-[color=blue]
            >Hi,
            >
            >Well, I tried this code:
            >
            >The best way to add a number of days to a date is:
            >---
            > time.setDate(ti me.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...[/color]

            That gives the wrong answer sometimes; but it is probably the cleanest
            way of doing what it actually does.
            [color=blue]
            >function dateAdd(baseStr ,days){
            > baseTime = baseStr.split("/");
            > time = new Date(baseTime[2],baseTime[1]-1,baseTime[0]);
            >
            > time = new Date(time.value Of()+days*1000* 3600*24);[/color]

            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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

            Comment

            • Dr John Stockton

              #7
              Re: Javascript Daylight time problem...

              JRS: In article <ismf6j85.fsf@h otpop.com>, seen in
              news:comp.lang. javascript, Lasse Reichstein Nielsen <lrn@hotpop.com >
              posted at Fri, 24 Oct 2003 12:08:42 :-[color=blue]
              >
              >Using the Date object will use the users timezone unless you change
              >it.[/color]

              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.demo n.co.uk/> - w. FAQish topics, links, acronyms
              PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
              Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

              Comment

              Working...