Announcement

Collapse
No announcement yet.

getMonth does not return proper month - Why?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • getMonth does not return proper month - Why?

    I have a simple code and was trying to test that my code will return bar's yr/mo/date vs today's yr/mo/date.

    However when I run it to get the month -getMonth()- it returns chart's bar Month as 7 and PCclock Month as 6. Today is Aug 1 and it appears correctly with debug of:

    today = new Date();
    BarDay = new Date(getValue("time",0));

    What did I do wrong? I ran it on 30 min charts as well as 1 min

    CODE:

    var todayBar;
    var today;
    var chartDOW; //<<Day of Week
    var PCclockDOW;
    var chartDay;
    var PCclockDay;

    var chartMon;
    var PCclockMon;
    var chartYr;
    var PCclockYr;



    function preMain(){
    setStudyTitle("Day Test");
    setShowCursorLabel(false);
    }

    function main(){




    //if (getBarState() == BARSTATE_NEWBAR ){

    today = new Date();
    BarDay = new Date(getValue("time",0));
    chartDOW = today.getDay();
    PCclockDOW = BarDay.getDay();
    chartDay = today.getDate();
    PCclockDay = BarDay.getDate();
    chartMon= today.getMonth();
    PCclockMon= BarDay.getMonth();
    chartYr= today.getYear();
    PCclockYr= BarDay.getYear();



    debugPrintln("==================================== ==================");
    debugPrintln("today==="+today);
    debugPrintln("todaybar==="+BarDay);

    debugPrintln(" today>PCclockDOW = "+PCclockDOW+" BarDay>chartDOW = "+chartDOW);
    debugPrintln(" today>PCclockDay = "+PCclockDay+" BarDay>chartDay = "+chartDay);
    debugPrintln(" today>PCclockMon = "+PCclockMon+" BarDay>chartMon = "+chartMon);
    debugPrintln(" today>PCclockYr = "+PCclockYr+" BarDay>chartYr = "+chartYr);



    //} //end barstate

    }
    Attached Files

  • #2
    Forgot to attach PNG of output

    Attached is the PNG of formula output. You can see the disparity.
    Attached Files

    Comment


    • #3
      Re: getMonth does not return proper month - Why?

      mrsdowfire
      The JavaScript Date Object numbers the months from 0 [January] to 11 [December]
      For further information on the Data Object see this article in the EFS KnowledgeBase
      Alex


      Originally posted by mrsdowfire
      I have a simple code and was trying to test that my code will return bar's yr/mo/date vs today's yr/mo/date.

      However when I run it to get the month -getMonth()- it returns chart's bar Month as 7 and PCclock Month as 6. Today is Aug 1 and it appears correctly with debug of:

      today = new Date();
      BarDay = new Date(getValue("time",0));

      What did I do wrong? I ran it on 30 min charts as well as 1 min

      CODE:

      var todayBar;
      var today;
      var chartDOW; //<<Day of Week
      var PCclockDOW;
      var chartDay;
      var PCclockDay;

      var chartMon;
      var PCclockMon;
      var chartYr;
      var PCclockYr;



      function preMain(){
      setStudyTitle("Day Test");
      setShowCursorLabel(false);
      }

      function main(){




      //if (getBarState() == BARSTATE_NEWBAR ){

      today = new Date();
      BarDay = new Date(getValue("time",0));
      chartDOW = today.getDay();
      PCclockDOW = BarDay.getDay();
      chartDay = today.getDate();
      PCclockDay = BarDay.getDate();
      chartMon= today.getMonth();
      PCclockMon= BarDay.getMonth();
      chartYr= today.getYear();
      PCclockYr= BarDay.getYear();



      debugPrintln("==================================== ==================");
      debugPrintln("today==="+today);
      debugPrintln("todaybar==="+BarDay);

      debugPrintln(" today>PCclockDOW = "+PCclockDOW+" BarDay>chartDOW = "+chartDOW);
      debugPrintln(" today>PCclockDay = "+PCclockDay+" BarDay>chartDay = "+chartDay);
      debugPrintln(" today>PCclockMon = "+PCclockMon+" BarDay>chartMon = "+chartMon);
      debugPrintln(" today>PCclockYr = "+PCclockYr+" BarDay>chartYr = "+chartYr);



      //} //end barstate

      }

      Comment


      • #4
        -

        mrsdowfire
        As an aside you are assigning the chat's time to the PClockXxx variables and the computer's time to the chartXxx variables ie reversed to what the names of the variables suggest they should be
        Alex

        Comment


        • #5
          mrsdowfire
          Also FWIW you can create a date object of the bar time just by using getValue("time") ie without the need of new Date(...)
          Lastly you may want to use getFullYear() rather than getYear() because the getYear() method of the Date Object will return a three digit number if the year is greater than 2000 (eg 110 for 2010). For more information on this you may want to review this article in the Core JavaScript Guide which is included in the EFS KnowledgeBase
          Alex

          Comment

          Working...
          X