Announcement

Collapse
No announcement yet.

number of days calculation

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

  • number of days calculation

    is there a way to calculate the number of days between two dates? ...ie numberDays = (date1 - date2).
    thanks,
    peter.

  • #2
    Hi Peter,

    Yes, here are several links that show how to code a function that will do this.

    http://www.breakingpar.com/bkp/home....256CC30078F008
    http://www.vijayjoshi.org/2008/10/24...in-javascript/
    http://www.breakingpar.com/bkp/home....25711000753DBF
    http://www.mcfedries.com/JavaScript/DaysBetween.asp

    Hope this helps.

    Steve

    Comment


    • #3
      hi steve,
      thank you so much. i have good success.
      i used a form of your 2nd example.

      function main() {
      nState = getBarState();
      if (nState == BARSTATE_NEWBAR) {
      var oneDay = 86400000; //24*60*60*1000
      var entryDate = new Date(2011,5,28); //June 28, 2011
      var today = new Date();
      var diffDays = Math.abs((entryDate.getTime() - today.getTime()) / (oneDay));

      debugPrintln(entryDate, "**", diffDays); // diffDays = 13.47846
      var numberDaysBack = Math.round(diffDays);
      debugPrintln(numberDaysBack); // diffDays = 13
      }
      }
      question. my entry date is 11 days back from todays date. the calculation shows 13 days i suspect this is due to the weekend? do you know how i could calculate the correct 11 days?
      i want to use the number in high(-11) so i can calculate the highest high going back to my entry date. does this make sense?

      thanks again steve,
      peter.

      Comment


      • #4
        Hi peter,

        You are most welcome, good first cut on the JavaScript code. I used the links as a resource and wrote a function that takes into account the weekends and provide the number of weekdays. Read the notes in the efs, they may be helpful.

        Hope this works for you.

        Steve


        Originally posted by peterjerome
        hi steve,
        thank you so much. i have good success.
        i used a form of your 2nd example.

        function main() {
        nState = getBarState();
        if (nState == BARSTATE_NEWBAR) {
        var oneDay = 86400000; //24*60*60*1000
        var entryDate = new Date(2011,5,28); //June 28, 2011
        var today = new Date();
        var diffDays = Math.abs((entryDate.getTime() - today.getTime()) / (oneDay));

        debugPrintln(entryDate, "**", diffDays); // diffDays = 13.47846
        var numberDaysBack = Math.round(diffDays);
        debugPrintln(numberDaysBack); // diffDays = 13
        }
        }
        question. my entry date is 11 days back from todays date. the calculation shows 13 days i suspect this is due to the weekend? do you know how i could calculate the correct 11 days?
        i want to use the number in high(-11) so i can calculate the highest high going back to my entry date. does this make sense?

        thanks again steve,
        peter.
        PHP Code:


        /*************************************************
         EFS Formula : weekdaysBetween
         Written By Steve Hare © 2011           
         written: 7/11/2011
         
         a demo efs that contains a function that will compute the number of weekdays 
         between two dates. When a date is used, the calculation assumes midnight that day 
         which is the beginning of the next day, so be aware of the basis of the calculation. I 
         included some debugPrintln output to illustrate this point.
         
         I also included some debugPrintln output to illustrate the difference in days and the difference in weeks.
         
         I used code at these links to develop this efs:
         [url]http://www.breakingpar.com/bkp/home...7256CC30078F008[/url]
         [url]http://www.vijayjoshi.org/2008/10/2...-in-javascript/[/url]
         [url]http://www.breakingpar.com/bkp/home...725711000753DBF[/url]
         [url]http://www.mcfedries.com/JavaScript/DaysBetween.asp[/url]

         posted in this thread: \"http://forum.esignalcentral.com/showthread.php?s=&threadid=36337\"
         Saved in FileShare \"http://share.esignal.com/groupgo.jsp?groupid=339\"

         *************************************************/
         
         
        function weekdaysBetween(date1date2) {
         var 
        adjustdiffDayseendDateone_daysstartDateweeksBetween;
         if (
        date1.indexOf("-") != -1) {
          
        date1 date1.split("-");
         } else if (
        date1.indexOf("/") != -1) {
          
        date1 date1.split("/");
         } else {
          return 
        0;
         }
         if (
        date2.indexOf("-") != -1) {
          
        date2 date2.split("-");
         } else if (
        date2.indexOf("/") != -1) {
          
        date2 date2.split("/");
         } else {
          return 
        0;
         }
         if (
        parseInt(date1[0], 10) >= 1000) {
          
        startDate = new Date(date1[0],date1[1],date1[2]);
         } else if (
        parseInt(date1[2], 10) >= 1000) {
          
        startDate = new Date(date1[2],date1[0],date1[1]);
         } else {
          return 
        0;
         }
         if (
        parseInt(date2[0], 10) >= 1000) {
          
        endDate = new Date(date2[0],date2[1],date2[2]);
         } else if (
        parseInt(date2[2], 10) >= 1000) {
          
        endDate = new Date(date2[2],date2[0],date2[1]);
         } else {
          return 
        0;
         }
         if (
        startDate endDate) {
          
        startDate;
          
        endDate;
         } else {
          
        endDate;
          
        startDate;
         }
         
        one_day 1000 60 60 24;
         
        diffDays Math.floor((s) / one_day);
         
        weeksBetween Math.floor(diffDays 7);
         
         
        debugPrintln("date1 = "+date1);
         
        debugPrintln("date2 = "+date2);
         
        debugPrintln("startDate = "+s);
         
        debugPrintln("endDate = "+e);debugPrintln("");
         
        debugPrintln("diffDays = "+diffDays);
         
        debugPrintln("weeksBetween = "+weeksBetween);debugPrintln("");

         if (
        s.getDay() == e.getDay()) {
          
        adjust 0;
         } else if (
        s.getDay() == && e.getDay() == 6) {
          
        adjust 5;
         } else if (
        s.getDay() == && e.getDay() == 0) {
          
        adjust 0;
         } else if (
        e.getDay() == || e.getDay() == 0) {
          
        adjust s.getDay();
         } else if (
        s.getDay() == || s.getDay() == 6) {
          
        adjust e.getDay();
         } else if (
        e.getDay() > s.getDay()) {
          
        adjust e.getDay() - s.getDay();
         } else {
          
        adjust e.getDay() - s.getDay();
         }
         return (
        weeksBetween 5) + adjust;
        }

        var 
        num=weekdaysBetween("06/01/2009","07/01/2009");
        debugPrintln("num weekdays = "+num);

        /* 
         * debug Output:
         * 
         * date1 = 06,01,2009
         * date2 = 07,01,2009
         * startDate = Wed Jul 01 2009 00:00:00 GMT-0400 (Eastern Daylight Time)
         * endDate = Sat Aug 01 2009 00:00:00 GMT-0400 (Eastern Daylight Time)
         * 
         * diffDays = 31
         * weeksBetween = 4
         * 
         * num weekdays = 22
         * 
         */ 

        Comment

        Working...
        X