none
Trying to create a 'days to go' calculator, but little success RRS feed

  • Question

  • I have had problems trying to create a program that would read a future date and subtract it from the current date to get the program to tell me the time difference in days. Thanks to a person called Burt from a separate forum, he suggested I used something like this.

     

    ~~

    Dat = Clock.Date

    Yr = Clock.Year

    Mth = Clock.Month

    Day = Clock.Day

     

    TextWindow.WriteLine("Current Status: "+Dat)

     

    FutYr = 2012

    FutMth = 02

    FutDay = 02

     

    TextWindow.WriteLine("Future Date: "+FutDay+"/"+FutMth+"/"+FutYr)

     

    DiffYr = FutYr - Yr

    DiffMth = FutMth - Mth

    DiffDay = FutDay - Day

     

    If Math.Remainder(Clock.year,4)=0 then

    DaysToWait = ((DiffYr * 366) + (DiffMth * 30) + (DiffDay))

    Else

    DaysToWait =((Diffyr*365)+(DiffMth*30)+(DiffDay))

    EndIF 

     

    TextWindow.ForegroundColor="yellow"

    TextWindow.WriteLine("No. of Days to Go: "+DaysToWait)

     

    ~~

    I have modified it slightly, to incorporate the leap years but we all know there are not 30 days in each month. That is the last thing to fix here.

    Could anyone help me figure out a way to tell the program what months are 28,29,30 or 31 days long  between the months that are been calculated.

    Thanks for any help in advance,



    Anthony :)
    Wednesday, December 28, 2011 9:50 PM

Answers

All replies

  • You could try using an array.

    Month[1] = 32 'Jan
    Month[2] = 28 'Feb or 29
    Month[3] = 31 'March
    Month[4] = 30 'April

    Something like that?

    Kirk.

    Wednesday, December 28, 2011 10:55 PM
  • Thanks for the idea and I think I'm making the program work, I think. I've only done a isolated test, meaning I'm just testing the days between the start of one month to the other. Your idea really helped me think deeper. Thanks plenty and I will ask for more help if need

    Sincerely, 


    Anthony :)
    Thursday, December 29, 2011 10:46 AM
  • Glad it helped, good look with your project and keep us informed on your progress.

    Kirk.

    Thursday, December 29, 2011 10:51 AM
  • Thanks for everyone's contribution. I have successfully fixed the error in my program. It now works 100% correctly. Thank you everyone and I hope you guys have a happy new years :)

    Sincerely


    Anthony :)
    • Edited by Tony93 Thursday, December 29, 2011 9:30 PM
    Thursday, December 29, 2011 9:29 PM
  • Good to hear you solved the problem Anthony,  would you care to publish your solution so that others can also have the benefit of your good work?

    Regards

    Bert

    Friday, December 30, 2011 12:05 AM
  • Sure. I have though found a slight problem with the code. I really thought it worked 100% correctly, but that's alright. There is always room for improvement. I'll publish what I got now and then later hopefully, I will publish the improvement.

    Here is the code you've all been waiting for:

     

    restart:

     

    TextWindow.ForegroundColor="white"

    TextWindow.Writeline(" ")

     

    Dat = Clock.Date

    Yr = Clock.Year

    Mth = Clock.Month

    Day = Clock.Day

     

    TextWindow.WriteLine("Current Status: "+Dat)

     

    futyr_option1=clock.Year

    futyr_option2=clock.Year+1 

     

    TextWindow.Write("Future Day: ")

    FutDay = TextWindow.ReadNumber()

    TextWindow.Write("Future Month: ")

    FutMth = TextWindow.ReadNumber()

    TextWindow.WriteLine("Future Year: Type in '1' for this Year or '2' for next year")

    Yr_Select= TextWindow.ReadNumber()

     

    If Yr_Select=1 Then

      FutYr=futyr_option1

    Else

      FutYr=futyr_option2

    EndIf

     

    TextWindow.WriteLine(" ")

    TextWindow.WriteLine("Future Date: "+FutDay+"/"+FutMth+"/"+FutYr)

     

    DiffYr = FutYr - Yr

    DiffMth = FutMth - Mth

    DiffDay = FutDay - Day

     

    '///--Calulating the Days between beginnings of month (inside the current year)

     

    TextWindow.Writeline(" ")

     

    start_m=Mth

    end_m=FutMth

     

    If Math.Remainder(Clock.year,4)=0 then 'If Current Year is  a Leap Year

      month2=29 'Feb

    Else

      month2=28 'Feb

    Endif 

     

      month1=31 'Jan

     

      month3=31 'Mar

      month4=30 'April

      month5=31 'May

      month6=30 'June

      month7=31 'July

      month8=31 'Aug

      month9=30 'Sep

      month10=31 'Oct

      month11=30 'Nov

      month12=31 'Dec

     

      If Math.Remainder(Clock.year+1,4)=0 Then 'If the Following Year is a Leap Year  

        month2_yr2=29

      Else

        month2_yr2=28

     EndIf

     

        'start_m=1

     

        If start_m=1 and end_m=1 then

          month_part=0

     

        ElseIf start_m=1 and end_m=2 then

          month_part=month1

     

        ElseIf start_m=1 and end_m=3 then

          month_part=month1+month2

     

        ElseIf start_m=1 and end_m=4 then

          month_part=month1+month2+month3

     

        ElseIf start_m=1 and end_m=5 then

          month_part=month1+month2+month3+month4

     

        ElseIf start_m=1 and end_m=6 then

          month_part=month1+month2+month3+month4+month5

     

        ElseIf start_m=1 and end_m=7 then

          month_part=month1+month2+month3+month4+month5+month6

     

        ElseIf start_m=1 and end_m=8 then

          month_part=month1+month2+month3+month4+month5+month6+month7

     

        ElseIf start_m=1 and end_m=9 then

          month_part=month1+month2+month3+month4+month5+month6+month7+month8

     

        ElseIf start_m=1 and end_m=10 then

          month_part=month1+month2+month3+month4+month5+month6+month7+month8+month9

     

        ElseIf start_m=1 and end_m=11 then

          month_part=month1+month2+month3+month4+month5+month6+month7+month8+month9+month10

     

        ElseIf start_m=1 and end_m=12 then

          month_part=month1+month2+month3+month4+month5+month6+month7+month8+month9+month10+month11

     

          'start_m=2

     

        ElseIf start_m=2 and end_m=1 then

          month_part=month2+month3+month4+month5+month6+month7+month8+month9+month10+month11+month12

     

        ElseIf start_m=2 and end_m=2 then

          month_part=0

     

        ElseIf start_m=2 and end_m=3 then

          month_part=month2

     

        ElseIf start_m=2 and end_m=4 then

          month_part=month2+month3

     

        ElseIf start_m=2 and end_m=5 then

          month_part=month2+month3+month4

     

        ElseIf start_m=2 and end_m=6 then

          month_part=month2+month3+month4+month5

     

        ElseIf start_m=2 and end_m=7 then

          month_part=month2+month3+month4+month5+month6

     

        ElseIf start_m=2 and end_m=8 then

          month_part=month2+month3+month4+month5+month6+month7

     

        ElseIf start_m=2 and end_m=9 then

          month_part=month2+month3+month4+month5+month6+month7+month8

     

        ElseIf start_m=2 and end_m=10 then

          month_part=month2+month3+month4+month5+month6+month7+month8+month9

     

        ElseIf start_m=2 and end_m=11 then

          month_part=month2+month3+month4+month5+month6+month7+month8+month9+month10

     

        ElseIf start_m=2 and end_m=12 then

          month_part=month2+month3+month4+month5+month6+month7+month8+month9+month10+month11

     

          'start_m=3

     

        ElseIf start_m=3 and end_m=1 then

          month_part=month3+month4+month5+month6+month7+month8+month9+month10+month11+month12

     

        ElseIf start_m=3 and end_m=2 then

          month_part=month3+month4+month5+month6+month7+month8+month9+month10+month11+month12+month1

     

        ElseIf start_m=3 and end_m=3 then

          month_part=0

     

        ElseIf start_m=3 and end_m=4 then

          month_part=month3

     

        ElseIf start_m=3 and end_m=5 then

          month_part=month3+month4

     

        ElseIf start_m=3 and end_m=6 then

          month_part=month3+month4+month5

     

        ElseIf start_m=3 and end_m=7 then

          month_part=month3+month4+month5+month6

     

        ElseIf start_m=3 and end_m=8 then

          month_part=month3+month4+month5+month6+month7

     

        ElseIf start_m=3 and end_m=9 then

          month_part=month3+month4+month5+month6+month7+month8

     

        ElseIf start_m=3 and end_m=10 then

          month_part=month3+month4+month5+month6+month7+month8+month9

     

        ElseIf start_m=3 and end_m=11 then

          month_part=month3+month4+month5+month6+month7+month8+month9+month10

     

        ElseIf start_m=3 and end_m=12 then

          month_part=month3+month4+month5+month6+month7+month8+month9+month10+month11

     

          'start_m=4

     

        ElseIf start_m=4 and end_m=1 then

          month_part=month4+month5+month6+month7+month8+month9+month10+month11+month12

     

        ElseIf start_m=4 and end_m=2 then

          month_part=month4+month5+month6+month7+month8+month9+month10+month11+month12+month1

     

        ElseIf start_m=4 and end_m=3 then

          month_part=month4+month5+month6+month7+month8+month9+month10+month11+month12+month1+month2_yr2

     

        ElseIf start_m=4 and end_m=4 then

          month_part=0

     

        ElseIf start_m=4 and end_m=5 then

          month_part=month4

     

        ElseIf start_m=4 and end_m=6 then

          month_part=month4+month5

     

        ElseIf start_m=4 and end_m=7 then

          month_part=month4+month5+month6

     

        ElseIf start_m=4 and end_m=8 then

          month_part=month4+month5+month6+month7

     

        ElseIf start_m=4 and end_m=9 then

          month_part=month4+month5+month6+month7+month8

     

        ElseIf start_m=4 and end_m=10 then

          month_part=month4+month5+month6+month7+month8+month9

     

        ElseIf start_m=4 and end_m=11 then

          month_part=month4+month5+month6+month7+month8+month9+month10

     

        ElseIf start_m=4 and end_m=12 then

          month_part=month4+month5+month6+month7+month8+month9+month10+month11

     

          'start_m=5

     

        ElseIf start_m=5 and end_m=1 then

          month_part=month5+month6+month7+month8+month9+month10+month11+month12

     

        ElseIf start_m=5 and end_m=2 then

          month_part=month5+month6+month7+month8+month9+month10+month11+month12+month1

     

        ElseIf start_m=5 and end_m=3 then

          month_part=month5+month6+month7+month8+month9+month10+month11+month12+month1+month2_yr2

     

        ElseIf start_m=5 and end_m=4 then

          month_part=month5+month6+month7+month8+month9+month10+month11+month12+month1+month2_yr2+month3

     

        ElseIf start_m=5 and end_m=5 then

          month_part=0

     

        ElseIf start_m=5 and end_m=6 then

          month_part=month5

     

        ElseIf start_m=5 and end_m=7 then

          month_part=month5+month6

     

        ElseIf start_m=5 and end_m=8 then

          month_part=month5+month6+month7

     

        ElseIf start_m=5 and end_m=9 then

          month_part=month5+month6+month7+month8

     

        ElseIf start_m=5 and end_m=10 then

          month_part=month5+month6+month7+month8+month9

     

        ElseIf start_m=5 and end_m=11 then

          month_part=month5+month6+month7+month8+month9+month10

     

        ElseIf start_m=5 and end_m=12 then

          month_part=month5+month6+month7+month8+month9+month10+month11

     

          'start_m=6

     

        ElseIf start_m=6 and end_m=1 then

          month_part=month6+month7+month8+month9+month10+month11+month12

     

        ElseIf start_m=6 and end_m=2 then

          month_part=month6+month7+month8+month9+month10+month11+month12+month1

     

        ElseIf start_m=6 and end_m=3 then

          month_part=month6+month7+month8+month9+month10+month11+month12+month1+month2_yr2

     

        ElseIf start_m=6 and end_m=4 then

          month_part=month6+month7+month8+month9+month10+month11+month12+month1+month2_yr2+month3

     

        ElseIf start_m=6 and end_m=5 then

          month_part=month6+month7+month8+month9+month10+month11+month12+month1+month2_yr2+month3+month4

     

        ElseIf start_m=6 and end_m=6 then

          month_part=0

     

        ElseIf start_m=6 and end_m=7 then

          month_part=month6

     

        ElseIf start_m=6 and end_m=8 then

          month_part=month6+month7

     

        ElseIf start_m=6 and end_m=9 then

          month_part=month6+month7+month8

     

        ElseIf start_m=6 and end_m=10 then

          month_part=month6+month7+month8+month9

     

        ElseIf start_m=6 and end_m=11 then

          month_part=month6+month7+month8+month9+month10

     

        ElseIf start_m=6 and end_m=12 then

          month_part=month6+month7+month8+month9+month10+month11

     

          'start_m=7

     

        ElseIf start_m=7 and end_m=1 then

          month_part=month7+month8+month9+month10+month11+month12

     

        ElseIf start_m=7 and end_m=2 then

          month_part=month7+month8+month9+month10+month11+month12+month1

     

        ElseIf start_m=7 and end_m=3 then

          month_part=month7+month8+month9+month10+month11+month12+month1+month2_yr2

     

        ElseIf start_m=7 and end_m=4 then

          month_part=month7+month8+month9+month10+month11+month12+month1+month2_yr2+month3

     

        ElseIf start_m=7 and end_m=5 then

          month_part=month7+month8+month9+month10+month11+month12+month1+month2_yr2+month3+month4  

     

        ElseIf start_m=7 and end_m=6 then

          month_part=month7+month8+month9+month10+month11+month12+month1+month2_yr2+month3+month4+month5

     

        ElseIf start_m=7 and end_m=7 then

          month_part=0

     

        ElseIf start_m=7 and end_m=8 then

          month_part=month7

     

        ElseIf start_m=7 and end_m=9 then

          month_part=month7+month8

     

        ElseIf start_m=7 and end_m=10 then

          month_part=month7+month8+month9

     

        ElseIf start_m=7 and end_m=11 then

          month_part=month7+month8+month9+month10

     

        ElseIf start_m=7 and end_m=12 then

          month_part=month7+month8+month9+month10+month11

     

          'start_m=8

     

        ElseIf start_m=8 and end_m=1 then

          month_part=month8+month9+month10+month11+month12

     

        ElseIf start_m=8 and end_m=2 then

          month_part=month8+month9+month10+month11+month12+month1

     

        ElseIf start_m=8 and end_m=3 then

          month_part=month8+month9+month10+month11+month12+month1+month2_yr2

     

        ElseIf start_m=8 and end_m=4 then

          month_part=month8+month9+month10+month11+month12+month1+month2_yr2+month3

     

        ElseIf start_m=8 and end_m=5 then

          month_part=month8+month9+month10+month11+month12+month1+month2_yr2+month3+month4  

     

        ElseIf start_m=8 and end_m=6 then

          month_part=month8+month9+month10+month11+month12+month1+month2_yr2+month3+month4+month5 

     

        ElseIf start_m=8 and end_m=7 then

          month_part=month8+month9+month10+month11+month12+month1+month2_yr2+month3+month4+month5+month6

     

        ElseIf start_m=8 and end_m=8 then

          month_part=0

     

        ElseIf start_m=8 and end_m=9 then

          month_part=month8

     

        ElseIf start_m=8 and end_m=10 then

          month_part=month8+month9

     

        ElseIf start_m=8 and end_m=11 then

          month_part=month8+month9+month10

     

        ElseIf start_m=8 and end_m=12 then

          month_part=month8+month9+month10+month11

     

          'start_m=9

     

        ElseIf start_m=9 and end_m=1 then

          month_part=month9+month10+month11+month12

     

        ElseIf start_m=9 and end_m=2 then

          month_part=month9+month10+month11+month12+month1

     

        ElseIf start_m=9 and end_m=3 then

          month_part=month9+month10+month11+month12+month1+month2_yr2

     

        ElseIf start_m=9 and end_m=4 then

          month_part=month9+month10+month11+month12+month1+month2_yr2+month3

     

        ElseIf start_m=9 and end_m=5 then

          month_part=month9+month10+month11+month12+month1+month2_yr2+month3+month4  

     

        ElseIf start_m=9 and end_m=6 then

          month_part=month9+month10+month11+month12+month1+month2_yr2+month3+month4+month5 

     

        ElseIf start_m=9 and end_m=7 then

          month_part=month9+month10+month11+month12+month1+month2_yr2+month3+month4+month5+month6

     

        ElseIf start_m=9 and end_m=8 then

          month_part=month9+month10+month11+month12+month1+month2_yr2+month3+month4+month5+month6+month7

     

        ElseIf start_m=9 and end_m=9 then

          month_part=0

     

        ElseIf start_m=9 and end_m=10 then

          month_part=month9

     

        ElseIf start_m=9 and end_m=11 then

          month_part=month9+month10

     

        ElseIf start_m=9 and end_m=12 then

          month_part=month9+month10+month11

     

          'start_m=10

     

        ElseIf start_m=10 and end_m=1 then

          month_part=month10+month11+month12

     

        ElseIf start_m=10 and end_m=2 then

          month_part=month10+month11+month12+month1

     

        ElseIf start_m=10 and end_m=3 then

          month_part=month10+month11+month12+month1+month2_yr2

     

        ElseIf start_m=10 and end_m=4 then

          month_part=month10+month11+month12+month1+month2_yr2+month3

     

        ElseIf start_m=10 and end_m=5 then

          month_part=month10+month11+month12+month1+month2_yr2+month3+month4  

     

        ElseIf start_m=10 and end_m=6 then

          month_part=month10+month11+month12+month1+month2_yr2+month3+month4+month5 

     

        ElseIf start_m=10 and end_m=7 then

          month_part=month10+month11+month12+month1+month2_yr2+month3+month4+month5+month6

     

        ElseIf start_m=10 and end_m=8 then

          month_part=month10+month11+month12+month1+month2_yr2+month3+month4+month5+month6+month7

     

        ElseIf start_m=10 and end_m=9 then

          month_part=month10+month11+month12+month1+month2_yr2+month3+month4+month5+month6+month7+month8

     

        ElseIf start_m=10 and end_m=10 then

          month_part=0

     

        ElseIf start_m=10 and end_m=11 then

          month_part=month10

     

        ElseIf start_m=10 and end_m=12 then

          month_part=month10+month11

     

          'start_m=11

     

        ElseIf start_m=11 and end_m=1 then

          month_part=month11+month12

     

        ElseIf start_m=11 and end_m=2 then

          month_part=month11+month12+month1

     

        ElseIf start_m=11 and end_m=3 then

          month_part=month11+month12+month1+month2_yr2

     

        ElseIf start_m=11 and end_m=4 then

          month_part=month11+month12+month1+month2_yr2+month3

     

        ElseIf start_m=11 and end_m=5 then

          month_part=month11+month12+month1+month2_yr2+month3+month4  

     

        ElseIf start_m=11 and end_m=6 then

          month_part=month11+month12+month1+month2_yr2+month3+month4+month5 

     

        ElseIf start_m=11 and end_m=7 then

          month_part=month11+month12+month1+month2_yr2+month3+month4+month5+month6

     

        ElseIf start_m=11 and end_m=8 then

          month_part=month11+month12+month1+month2_yr2+month3+month4+month5+month6+month7

     

        ElseIf start_m=11 and end_m=9 then

          month_part=month11+month12+month1+month2_yr2+month3+month4+month5+month6+month7+month8

     

        ElseIf start_m=11 and end_m=10 then

          month_part=month11+month12+month1+month2_yr2+month3+month4+month5+month6+month7+month8+month9

     

        ElseIf start_m=11 and end_m=11 then

          month_part=0

     

        ElseIf start_m=11 and end_m=12 then

          month_part=month11

     

          'start_m=12

     

        ElseIf start_m=12 and end_m=1 then

          month_part=month12 

     

        ElseIf (start_m=12 and end_m=2) then

          month_part=month12+month1 

     

        ElseIf start_m=12 and end_m=3 then

          month_part=month12+month1+month2_yr2 

     

        ElseIf start_m=12 and end_m=4 then

          month_part=month12+month1+month2_yr2+month3 

     

        ElseIf start_m=12 and end_m=5 then

          month_part=month12+month1+month2_yr2+month3+month4  

     

        ElseIf start_m=12 and end_m=6 then

          month_part=month12+month1+month2_yr2+month3+month4+month5 

     

        ElseIf start_m=12 and end_m=7 then

          month_part=month12+month1+month2_yr2+month3+month4+month5+month6

     

        ElseIf start_m=12 and end_m=8 then

          month_part=month12+month1+month2_yr2+month3+month4+month5+month6+month7

     

        ElseIf start_m=12 and end_m=9 then

          month_part=month12+month1+month2_yr2+month3+month4+month5+month6+month7+month8

     

        ElseIf start_m=12 and end_m=10 then

          month_part=month12+month1+month2_yr2+month3+month4+month5+month6+month7+month8+month9

     

        ElseIf start_m=12 and end_m=11 then

          month_part=month12+month1+month2_yr2+month3+month4+month5+month6+month7+month8+month9+month10 

     

        ElseIf start_m=12 and end_m=12 then

          month_part=0

     

        Else

     

          unavaliable:

     

          TextWindow.ForegroundColor="red"

          TextWindow.WriteLine(" ")

          TextWindow.WriteLine("Sorry but inserted date has passed. Please try a future date.")

     

          TextWindow.WriteLine(" ")

          TextWindow.WriteLine("Month Part: "+month_part)

          TextWindow.WriteLine("Diff Day: "+DiffDay)

     

          Goto restart

     

        EndIf

     

      '///--End of Calculating days difference between beginnings of months.

     

    If Math.Remainder(Clock.year+1,4)=0 then 'If leap year for next year is true

      DaysToWait = (month_part)+(DiffDay)

    Else

      DaysToWait = (month_part)+(DiffDay) 'If it isn't true

    EndIf  

     

    If DaysToWait<0 then

      Goto unavaliable

    EndIf

     

    TextWindow.ForegroundColor="yellow"

    TextWindow.WriteLine("No. of Days to Go: "+DaysToWait)

     

    TextWindow.WriteLine(" ")

     

     

    Goto restart

     

    Enjoy, sincerely


    Anthony :)
    Monday, January 2, 2012 10:30 AM
  • There is a publish button in the panel above the editor space. It will give you a code and a link to your published program.
    ~~AirWaves!!~~
    Monday, January 2, 2012 11:39 PM
  • Well done Anthony,   I copied your code into my editor and it worked fine, gave correct answer for every date I tried for this (1) year.  However when I put in a date for next (2) year, it correctly showed the year as 2013 but gave the number of days that would have been correct for this year. I'm sure that will be a small problem to fix and admire your tenacity, you have certainly put in the hard work.

    Regards

    Bert

    Tuesday, January 3, 2012 12:09 AM
  • Thanks. Its great when things fall in place correctly. It took some time but it works. Thanks for the help everyone :)
    Anthony :)
    Wednesday, January 4, 2012 8:20 AM
  • Hi Anthony,

    You're doing a great job to make the program to work, even there is still a bug to fix. But I think you can put your hard work into good coding experience if you can refractor your code and make it more efficient.

    Do you think you would take that challenge?

    Thursday, January 5, 2012 4:08 PM
  • I will take the challenge. I'll update you and everyone else in this forum about it. Thanks for the challenge.
    Anthony :)
    Friday, January 6, 2012 7:12 PM
  • Hi everyone. It has been ages but no, I have not done the challenge. Instead, I am going to progress with my work by first learning C and C++. But, thanks again everyone for their guidance and help along the way :)

    Sincerely, Anthony :)

    Tuesday, October 23, 2012 10:24 PM
  • Tony93, much good luck for you on your journey to learn Dennis MacAlistair Ritchie's language and its breeds!

    If you can, show up here other times!

    P.S.: Try out Java as well. It's an excellent cross-platform alternative on C. An easy API for it is Processing, which I'm trying to learn now!  :P


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Wednesday, October 24, 2012 3:42 AM
    Answerer
  • In the thread "Days since caculation ..." here I gave a subroutine of about 30 lines which can calculate the difference in days for any two dates between march 1 1900  and february 28 2100. The subroutine is completely in Small Basic. 

    I hope someone can use this


    Jan [ WhTurner ] The Netherlands


    Wednesday, October 24, 2012 5:46 PM
    Answerer
  • You could try using an array.

    Month[1] = 32 'Jan
    Month[2] = 28 'Feb or 29
    Month[3] = 31 'March
    Month[4] = 30 'April

    Something like that?

    Kirk.


    5777
    Sunday, December 16, 2012 10:20 PM