Android Question DateUtils.PeriodBetween help please for a countdown timer?

Mashiane

Expert
Licensed User
Longtime User
Hi

I am trying to count down to a particular future date. For example, today is August 30, 2016 at 17:00, my future date is October 1, 2016 at 15:00

I want to do a timer with a countdown every second of how many days, hours, minutes and seconds from today i.e. August 30, 29016 until October 1, 2016

This is what I have done currently.

B4X:
Dim speriod as period
speriod = DateUtils.PeriodBetween(sdatetimeEnd,DateTime.now)
log(speriod)

and the output is...

B4X:
[Days=20, Hours=17, IsInitialized=true
, Minutes=25, Months=7, Seconds=10
, Years=0]

Surely this is incorrect. It cant be 20 days as September has 30 days in itself and hours cant be 17 because thats the current time. I have done this on the timer..

B4X:
shh = speriod.Hours
    smm = speriod.Minutes
    smi = speriod.Seconds
    sdy = speriod.Days
    smi = 60 - smi
    smm = 60 - smm
    shh = 24 - shh

Can someone please advise me? Thanks...
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
  DateTime.DateFormat =   "dd.MM.yyyy"   
   DateTime.TimeFormat =   "HH:mm"   
   Dim dest As Long = DateTime.DateTimeParse("1.10.2016","15:00")
   Log(DateTime.Date(dest))
   Log(DateTime.Time(dest))
   Dim speriod As Period
   speriod = DateUtils.PeriodBetween(DateTime.now,dest)
   Log(speriod)

** Activity (main) Create, isFirst = true **
log only in release mode
01.10.2016
15:00
[Days=0, Hours=21, IsInitialized=true
, Minutes=1, Months=1, Seconds=7
, Years=0]
Erel
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Looks good for me
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
B4X:
  DateTime.DateFormat =   "dd.MM.yyyy"  
   DateTime.TimeFormat =   "HH:mm"  
   Dim dest As Long = DateTime.DateTimeParse("1.10.2016","15:00")
   Log(DateTime.Date(dest))
   Log(DateTime.Time(dest))
   Dim speriod As Period
   speriod = DateUtils.PeriodBetween(DateTime.now,dest)
   Log(speriod)

Looks good for me
Thanks a lot, I found my mistake, I had use DateParse instead of DateTimeParse and also the PeriodBetween assignment was wrong. You are a star!
 
Upvote 0
Top