Android Question Weekly countdown timer

Almora

Well-Known Member
Licensed User
Longtime User
Hello. I am using this code.

2018 01 14 21:30
I calculated the remaining time.

how to count down every week at 21:30 hours.


B4X:
EditText1.Text=2018  'years
EditText2.Text=1     ' months
EditText3.Text=14     'days
EditText4.Text=21      'hours
EditText5.Text=30       'minutes


    Timer1.Initialize("Timer1", 1000)
    Timer1.Enabled = True

target= DateUtils.SetDateAndTime(EditText1.Text,EditText2.Text,EditText3.Text,EditText4.Text,EditText5.Text,00)
   
   
   
    ******************************************
   
   
   
Sub Timer1_Tick
   
    periode1 = DateUtils.PeriodBetween(DateTime.Now,target)
    Label1.Text = periode1.Years & " years " & periode1.Months & " months " & periode1.Days &" days "&CRLF&CRLF & periode1.Hours &" hours "& periode1.Minutes &" minutes "&periode1.Seconds &" seconds"
   
   
End Sub
 

Almora

Well-Known Member
Licensed User
Longtime User
B4X:
 If periode1.Hours=0  And periode1.Minutes= 0  And periode1.Seconds= 0 Or periode1.Seconds<0 Then
 
' weekly repeat = ?????
 
 Else
 Label1.Text = periode1.Years & " years " & periode1.Months & " months " & periode1.Days &" days "&CRLF&CRLF & periode1.Hours &" hours "& periode1.Minutes &" minutes "&periode1.Seconds &" seconds"
  

 End If

I tried it but it did not.
 
Upvote 0

Almora

Well-Known Member
Licensed User
Longtime User
EditText1.Text=2018 'years
EditText2.Text=1 ' months
EditText3.Text=14 'days
EditText4.Text=21 'hours
EditText5.Text=30 'minutes

2018
01/14 21:30
I calculated the remaining time.
as in the picture..
PicsArt_01-14-10.07.52.jpg

I want to repeat every week when the rest of the time is over.
how do you count down a countdown every week Sunday at 21:30?

every Sunday at 21:30 will repeat the countdown
 
Upvote 0

udg

Expert
Licensed User
Longtime User
every Sunday at 21:30 will repeat the countdown
Do you mean that once you run your app it should show a countdown starting at current time toward the NEXT Sunday at 21:30?
Or is it an ever running display that resets itself each Sunday at 21:30 to update the countdown for next occurence?

Another option: is it kind of an alarm? The user sets day and time and your app shows a countdown.
If you close it and then start it again you would like to see the running countdown.
Anyway a service will check on countdown reaching its end and reset everything for next schedule?
If this is the case there are a few examples on the forum; try seraching for "next schedule" or something like that.
 
Upvote 0

Almora

Well-Known Member
Licensed User
Longtime User
yes..
Or is it an ever running display that resets itself each Sunday at 21:30 to update the countdown for next occurence?

every Sunday at 21:30 the gray count will reset and start again. This will repeat every Sunday.

It's not an alarm.
 
Last edited:
Upvote 0

udg

Expert
Licensed User
Longtime User
Since @Erel was quicker than me..here it is a function to find the next day-of-week from the specified date. Could be useful sooner or later..
B4X:
'Returns next DayOfWeek from date t1
Sub NextDow(t1 As Long, dow As Int) As Long
   Dim ddow As Int = DateTime.GetDayOfWeek(t1) '1=sun, 2 = mon,..
   Dim p1 As Period
   p1.Initialize
   p1.Days=1
   Do While ddow <> dow Then
       t1 = DateUtils.AddPeriod(t1,p1)
       ddow = DateTime.GetDayOfWeek(t1)
   Loop
   Return t1
End Sub

'call as
DateTime.DateFormat="dd/MM/yyyy"                   'set to what you need, but recall to save current one
Dim t1 As Long = DateTime.DateParse("12/01/2018")  'set this according to DateFormat above
Dim t2 As Long = NextDow(t1,1)                     'look for next Sunday; change 1 to 2 for Monday and so on
Log(DateTime.Date(t2))

ps: code not tested nor optimized; just a quick note/starting point to keep in case it will be ever needed
 
Upvote 0

Almora

Well-Known Member
Licensed User
Longtime User
B4X:
If DateTime.Now > target Then
 Dim p As Period
 p.Days = 7
 target = DateUtils.AddPeriod(p)
End If




B4X:
---------------------------------------------

'    EditText1.Text= DateTime.GetYear(DateTime.Now)
    EditText1.Text= 2018

'    EditText2.Text= DateTime.GetMonth(DateTime.Now)
    EditText2.Text= 2

'    EditText3.Text= DateTime.GetDayOfMonth(DateTime.Now)
    EditText3.Text= 5

'    EditText4.Text= DateTime.GetHour(DateTime.Now)
    EditText4.Text= 13

'    EditText5.Text= DateTime.GetMinute(DateTime.Now)
    EditText5.Text= 00

    
    target= DateUtils.SetDateAndTime(EditText1.Text,EditText2.Text,EditText3.Text,EditText4.Text,EditText5.Text,00)
    
    If DateTime.Now>target Then
    periode1.Days=7
    target=DateUtils.AddPeriod(target,periode1)
        DateUtils.TicksToString(target)
    End If


--------------------------------------

periode1 = DateUtils.PeriodBetween(DateTime.Now,target)

Label4.Text = periode1.Years & " Years - " & periode1.Months & " Months - " & periode1.Days &" Days - "&CRLF&CRLF & periode1.Hours &" Hours - "& periode1.Minutes &" Minutes - "&periode1.Seconds &" Seconds "


I am using this code. the code is not working properly after two weeks. Date: 2018 02 05 13:00 is counting down until one week's completion. A week later, the countdown is reset to 2018 02 12 13:00 and is okay. Again a week later, he counts down to 2018 02 19 13:00. but he is not counting down next week. there is an error. Thank you
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Shouldn't it be
B4X:
Do While DateTime.Now >= target Then ..
in order to work in that rare case when the code is executed exactly at the time set as the target?
Reading post #9 it seems to me that he could have a countdown to zero while next target won't be set to next week due to above reason.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
I agree. They say "Luck is blind while bad luck has got excellent sight" (or something similar), so accounting for that "bad" millisecond.. :)
 
Last edited:
Upvote 0

Almora

Well-Known Member
Licensed User
Longtime User
B4X:
Do While DateTime.Now >= target Then
 Dim p As Period
 p.Days = 7
 target = DateUtils.AddPeriod(p)
Loop
This way it will handle cases where the

B4X:
periode1.Minutes=1

I try and look good. I tried minutes instead of weeks. the next minute there was no problem.
 
Upvote 0
Top