Android Question Date/Time picker for past 24 hours

trueboss323

Active Member
Licensed User
Longtime User
Hi, is there a way to have a Date/Time chooser so I can limit user to only choose a timeframe within the past 24 hours?
 
Last edited:

udg

Expert
Licensed User
Longtime User
Search the forum for a modified version of ADP (AnotherDatePicker) where you can set start/end date. This will hopefully help to solve the date part of your question.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Hi, is there a way to have a Date/Time chooser so I can limit user to only choose a timeframe within the past 24 hours?
One way to do this is to check if the date and time you get from the date picker falls within the last 24 hours:
B4X:
Log(ValidateDate("1/16/2018","15:40:12") )   'returns true   code run 1/16/2018 around 19:30
    Log(ValidateDate("1/15/2018","15:40:12") )  'returns false
    Log(ValidateDate("1/17/2018","9:45:17") )  'returns false
    Log(ValidateDate("1/16/2018","1:05:07") )  'returns true

Sub ValidateDate(MyDate As String, MyTime As String) As Boolean
    Dim p As Period
    p.Days = -1
    Dim DateStart As Long = DateUtils.AddPeriod(DateTime.Now, p)
    Dim DateEnd As Long=DateTime.Now
    Dim DT As Long=DateTime.DateTimeParse(MyDate,MyTime)
    If DT>=DateStart And DT<=DateEnd Then Return True Else Return False   
End Sub[/code
You can modify the code if the date you extract from picker is in ticks or you are only dealing with a date and no time.
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
I was just writing something like that @Mahares. I was looking to use the following sub with DateUtils.AddPeriod(DateTime.Now, -<Period with 24 hours>) to check the 24 hour time on setting the time.
B4X:
Sub Time_onTimeSet(hour As Int,minute As Int, second As Int)

Goodnight...
 
Upvote 0

trueboss323

Active Member
Licensed User
Longtime User
I ended up using the Switch Date and Time picker library I like it since it allows the user to select both date and time in one dialog. However the result it returns the date/time separately as: year, month, day, hour, minute. It also can give a full date as "Wed Jan 24 08:20:40 MST 2018" but doesn't give a full time. So how can i use these to work with Mahares' code ?
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
However the result it returns the date/time separately as: year, month, day, hour, minute
This is how I would then do it. Hope this is compatible with your preference:
B4X:
Log(ValidateMyDate(2018,1, 23, 17, 10, 0) )  'true   code run around 6 PM EST
    Log(ValidateMyDate(2018,1, 22, 17, 10, 0) )   'false
    Log(ValidateMyDate(2018,1, 23, 1, 0, 0) )   'true

Sub ValidateMyDate(y As Int,m As Int, d As Int, h As Int, n As Int, s As Int) As Boolean
    Dim p As Period
    p.Days = -1
    Dim DateStart As Long = DateUtils.AddPeriod(DateTime.Now, p)
    Dim DateEnd As Long=DateTime.Now   
    Dim DT As Long=DateUtils.SetDateAndTime(y,m,d,h,n,s)
    If DT>=DateStart And DT<=DateEnd Then Return True Else Return False   
End Sub
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I have been testing your code for a while now. It has been working great so far. Thank you!

That is good. You probably figured out already that, if you do not want to deal with the seconds, you can make a change like this:
B4X:
Sub ValidateMyDate(y As Int,m As Int, d As Int, h As Int, n As Int) As Boolean
.
.
Dim DT As Long=DateUtils.SetDateAndTime(y,m,d,h,n,0)
 
Upvote 0
Top