Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #19529 > unrolled thread

How do I add new currentTime to an array

Started by"Tony" <tony.johansson@inport.com>
First post2013-04-29 16:47 +0200
Last post2013-04-30 22:31 +0100
Articles 3 — 2 participants

Back to article view | Back to comp.lang.javascript


Contents

  How do I add new currentTime to an array "Tony" <tony.johansson@inport.com> - 2013-04-29 16:47 +0200
    Re: How do I add new currentTime to an array "Tony" <tony.johansson@inport.com> - 2013-04-29 16:53 +0200
    Re: How do I add new currentTime to an array Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-04-30 22:31 +0100

#19529 — How do I add new currentTime to an array

From"Tony" <tony.johansson@inport.com>
Date2013-04-29 16:47 +0200
SubjectHow do I add new currentTime to an array
Message-ID<klm0vg$545$1@dont-email.me>
I have a small problem. I change the variable currentTime in the for loop in 
function GenerateTableHeader() and I want to add this new value to the array 
timeArray.
When I add now to the array timeArray I can see that all values is the same.
I have thought that this algoritm would work but obviously it doesn't.

There must be something that I have missed here.
var temp = currentTime;
            timeArray[i] = temp;
            temp ="";


function GenerateTableHeader()
    {
        var cellTitle,
            hours,
            minutes,
            tempDate;

        hours = currentTime.getHours();
        minutes = currentTime.getMinutes();

        switch (currentInterval)
        {
            case "Interval.RdoHour":
                currentTime.setHours(currentTime.getHours() - 1);
                break;

           case "Interval.RdoHalv":
                currentTime.setMinutes((parseInt((minutes) / 30) * 30) % 
60);
                currentTime.setMinutes(currentTime.getMinutes() - 30);
                 break;

           case "Interval.RdoKvart":
                currentTime.setMinutes((parseInt((minutes) / 15) * 15) % 
60);
                currentTime.setMinutes(currentTime.getMinutes() - 15);
                break;
        }

        for (var i = -1; i < antalColumner; i++)
        {
            var tempDate

           if (i == -1)
           {
               var elem = document.getElementById("objectName").innerHTML = 
"Object";
           }
           else
           {
              if (currentInterval == "Interval.RdoHour")
              {
                  currentTime.setHours(currentTime.getHours() + 1);
                  hours = currentTime.getHours();
                  minutes = currentTime.getMinutes();
                  cellTitle = (hours < 10 ? "0" + hours : hours) + ":00";
              }
              else if (currentInterval == "Interval.RdoHalv")
              {
                  currentTime.setMinutes(currentTime.getMinutes() + 30);
                  minutes = currentTime.getMinutes();
                  hours = currentTime.getHours();
                  cellTitle = (hours < 10 ? "0" + hours : hours) + ":" + 
(minutes < 10 ? "0" + minutes : minutes);
              }
              else if (currentInterval == "Interval.RdoKvart")
              {
                  currentTime.setMinutes(currentTime.getMinutes() + 15);
                  minutes = currentTime.getMinutes();
                  hours = currentTime.getHours();
                  cellTitle = (hours < 10 ? "0" + hours : hours) + ":" + 
(minutes < 10 ? "0" + minutes : minutes);
              }

              var item = 'item'+i;
              var elem = document.getElementById(item).innerHTML = 
cellTitle;

              var temp = currentTime;
              timeArray[i] = temp;
              temp ="";
           }
        }
    }

//Tony 

[toc] | [next] | [standalone]


#19530

From"Tony" <tony.johansson@inport.com>
Date2013-04-29 16:53 +0200
Message-ID<klm1c3$7os$1@dont-email.me>
In reply to#19529

"Tony"  wrote in message news:klm0vg$545$1@dont-email.me...

I have a small problem. I change the variable currentTime in the for loop in
function GenerateTableHeader() and I want to add this new value to the array
timeArray.
When I add now to the array timeArray I can see that all values is the same.
I have thought that this algoritm would work but obviously it doesn't.

There must be something that I have missed here.
var temp = currentTime;
            timeArray[i] = temp;
            temp ="";


function GenerateTableHeader()
    {
        var cellTitle,
            hours,
            minutes,
            tempDate;

        hours = currentTime.getHours();
        minutes = currentTime.getMinutes();

        switch (currentInterval)
        {
            case "Interval.RdoHour":
                currentTime.setHours(currentTime.getHours() - 1);
                break;

           case "Interval.RdoHalv":
                currentTime.setMinutes((parseInt((minutes) / 30) * 30) %
60);
                currentTime.setMinutes(currentTime.getMinutes() - 30);
                 break;

           case "Interval.RdoKvart":
                currentTime.setMinutes((parseInt((minutes) / 15) * 15) %
60);
                currentTime.setMinutes(currentTime.getMinutes() - 15);
                break;
        }

        for (var i = -1; i < antalColumner; i++)
        {
            var tempDate

           if (i == -1)
           {
               var elem = document.getElementById("objectName").innerHTML =
"Object";
           }
           else
           {
              if (currentInterval == "Interval.RdoHour")
              {
                  currentTime.setHours(currentTime.getHours() + 1);
                  hours = currentTime.getHours();
                  minutes = currentTime.getMinutes();
                  cellTitle = (hours < 10 ? "0" + hours : hours) + ":00";
              }
              else if (currentInterval == "Interval.RdoHalv")
              {
                  currentTime.setMinutes(currentTime.getMinutes() + 30);
                  minutes = currentTime.getMinutes();
                  hours = currentTime.getHours();
                  cellTitle = (hours < 10 ? "0" + hours : hours) + ":" +
(minutes < 10 ? "0" + minutes : minutes);
              }
              else if (currentInterval == "Interval.RdoKvart")
              {
                  currentTime.setMinutes(currentTime.getMinutes() + 15);
                  minutes = currentTime.getMinutes();
                  hours = currentTime.getHours();
                  cellTitle = (hours < 10 ? "0" + hours : hours) + ":" +
(minutes < 10 ? "0" + minutes : minutes);
              }

              var item = 'item'+i;
              var elem = document.getElementById(item).innerHTML =
cellTitle;

              var temp = currentTime;
              timeArray[i] = temp;
              temp ="";
           }
        }
    }

//Tony

I found the solution
   var temp = new Date(currentTime.valueOf())
              timeArray[i] = temp;

//Tony 

[toc] | [prev] | [next] | [standalone]


#19548

FromDr J R Stockton <reply1300@merlyn.demon.co.uk.invalid>
Date2013-04-30 22:31 +0100
Message-ID<q2UMG7RggDgRFwIL@invalid.uk.co.demon.merlyn.invalid>
In reply to#19529
In comp.lang.javascript message <klm0vg$545$1@dont-email.me>, Mon, 29
Apr 2013 16:47:17, Tony <tony.johansson@inport.com> posted:

>           case "Interval.RdoHour":
>               currentTime.setHours(currentTime.getHours() - 1);
>               break;

That code might well give an unexpected result in one particular hour in
Spring, when one hour of LCT is omitted, and in the Autumn, when one is
duplicated.  Worse might happen if your OS was set for LHI.

If you want always to go back by one SI hour, use of setTime should be
more efficient.

The newsgroup should like to know what source you are attempting to
learn JavaScript from; it should evidently be avoided.

-- 
 (c) John Stockton, nr London, UK.   E-mail, see Home Page.    Turnpike v6.05.
 Website  <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
 PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
 Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web