Android Question Minutes until midnight

Derek Jee

Active Member
Licensed User
Longtime User
Hi there

Anyone know a good way of determining how many minutes there are from 'now' to midnight tonight? Actually I think my question is how to get the date/time for midnight tonight and use it to compare with the date/time now as I can already compare two dates..

Many thanks,

Derek.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this sub:
B4X:
Sub MinutesTillMidnight As Int
   Dim midnight As Long = DateUtils.SetDate(DateTime.GetYear(DateTime.Now), _
     DateTime.GetMonth(DateTime.Now), DateTime.GetDayOfMonth(DateTime.Now))
   midnight = DateTime.Add(midnight, 0, 0, 1)
   Dim p As Period = DateUtils.PeriodBetweenInDays(DateTime.Now, midnight)
   Return p.Hours * 60 + p.Minutes
End Sub
 
Upvote 0

Ed Brown

Active Member
Licensed User
Longtime User
..and another way is
B4X:
    ' Make sure the date format does not include time
    DateTime.DateFormat = DateTime.DeviceDefaultDateFormat
   
    ' Add 1 day to today and parse the date this will give you midnight
    ' Then subtract the current time
    Dim MinsToMidnight As Long
    MinsToMidnight = DateTime.DateParse(DateTime.Date(DateTime.Add(DateTime.Now, 0, 0, 1))) - DateTime.Now
   
    ' Calculate the minutes from now until midnight
    MinsToMidnight = MinsToMidnight / DateTime.TicksPerMinute
    Log(MinsToMidnight)
 
Upvote 0
Top