Android Question Simple date scrolling a text entry by plus/minus one or two days

JonnyCav

Member
I'm trying to enable a user to simply press a plus or minus button to move the date up or down. I do not want a DatePicker becuase it will only be for a couple of days either way.
The 'Log' results are perfect but the txtDate (perfect on entry) becomes ticks when the button is clicked
I'd like to allow multiple clicks (i.e. -1, -2, -3 )
Here's my code

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    DateTime.DateFormat="dd-MMM-yyyy"
    Dim cdate As String
    cdate=DateTime.Date(DateTime.Now)
    txtDate.Text=cdate
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Private Sub btnPlus_Click
    Dim plusDay As Long
    DateTime.DateFormat="dd-MMM-yyyy"
    plusDay = DateTime.Add(DateTime.Now, 0, 0, 1)
    Log("Tomorrow's date is: " & DateTime.Date(plusDay))
    txtDate.Text=plusDay
End Sub

Private Sub btnMinus_Click
    Dim minusDay As Long
    
    minusDay = DateTime.Add(DateTime.Now, 0, 0, -1)
    DateTime.DateFormat="dd-MMM-yyyy"
        
    Log("Yesterday's date was: " & DateTime.Date(minusDay))
    txtDate.Text=minusDay
End Sub

Any ideas where I'm fouling up?
 

mangojack

Well-Known Member
Licensed User
Longtime User
Typing on phone, so in short...

B4X:
 txtDate.Text=plusDay

Should be...
B4X:
 txtDate.Text=DateTime.Date(plusDay)

Same as your Log statement


And if you are not changing DateFormat elsewhere In the app, you can remove them from the plus minus subs...
You have previously already set the format.
 
Upvote 0

emexes

Expert
Licensed User
I'd like to allow multiple clicks (i.e. -1, -2, -3 )

Instead of always basing the plus/minus on today ie DateTime.Now, base it on the date currently shown in the text entry ie DateTime.Parse(txtDate.Text)

which you'll have to wrap in a Try ... Catch in case the user manually changes/edits the text entry to be an invalid date or format

of, probably simpler, have a local global Dim txtDateTicks As Long that you initialize in Activity_Create to DateTime.Now, and then apply plus/minus to that

although if the user is allowed to manually change the text entry, presumably you'll still need DateTime.Parse(txtDate.Text) to get the final result
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
user to simply press a plus or minus button to move the date up or down. I do not want a DatePicker becuase it will only be for a couple of days either way.
Another more elegant way than having to have code for two separate buttons is to use B4XPlusMinus of Xui Views lib.. Here is an example
B4X:
    PlusMinus.SetNumericRange(-4,4,1)     '4 days either way. You can change it
    PlusMinus.lblPlus.Color=xui.Color_Green
    PlusMinus.lblMinus.Color=xui.Color_Red
    PlusMinus.SelectedValue = 0   'starting
    DateTime.DateFormat="dd-MMM-yyyy"

Sub PlusMinus_ValueChanged (Value As Object)
    Dim plusorminusDay As Long
    plusorminusDay = DateTime.Add(DateTime.Now, 0, 0, Value)
    Log(DateTime.Date(plusorminusDay))  'apply this to your txtDate.Text
End Sub
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
this is how i would do it.

B4X:
Sub Button1_Click 'add 1 day
    Dim currentDate As Long = DateTime.DateParse(txtDate.Text)
    txtDate.Text = DateTime.Date(DateTime.Add(currentDate,0,0,1))
End Sub

Private Sub Button2_Click 'subtract 1 day
    Dim currentDate As Long = DateTime.DateParse(txtDate.Text)
    txtDate.Text = DateTime.Date(DateTime.Add(currentDate,0,0,-1))
End Sub

you cannot always use Datetime.now because it will only add 1 day to the current date. you need to parse the date from txtDate each time
 
Upvote 0

emexes

Expert
Licensed User
you cannot always use Datetime.now because it will only add 1 day to the current date.
I am with you on this

this is how i would do it.
B4X:
Sub Button1_Click 'add 1 day
    Dim currentDate As Long = DateTime.DateParse(txtDate.Text)
    txtDate.Text = DateTime.Date(DateTime.Add(currentDate,0,0,1))
but what if the user has edited txtDate to something invalid that causes DateParse to choke?
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
I am with you on this


but what if the user has edited txtDate to something invalid that causes DateParse to choke?
first i thought txtDate is a label so the date is taken from somewhere and you only add/subtract a day from it. so no way invalid entries.
BUT if you want to let the user enter a date just check for validation -> if not ok then enter a valid date like datetime.now

B4X:
Private Sub checkValidDate
    Dim validDateReg As String = $"(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$"$
    If Not(Regex.IsMatch(validDateReg,TextField1.Text)) Then
        TextField1.Text = DateTime.Date(DateTime.Now)
        Log("invalid date entered")
    End If   
End Sub
 
Private Sub Button3_Click
    checkValidDate
End Sub

after user enters the date and click confirm or want to save it you make the validation.
 
Upvote 0

JonnyCav

Member
this is how i would do it.

B4X:
Sub Button1_Click 'add 1 day
    Dim currentDate As Long = DateTime.DateParse(txtDate.Text)
    txtDate.Text = DateTime.Date(DateTime.Add(currentDate,0,0,1))
End Sub

Private Sub Button2_Click 'subtract 1 day
    Dim currentDate As Long = DateTime.DateParse(txtDate.Text)
    txtDate.Text = DateTime.Date(DateTime.Add(currentDate,0,0,-1))
End Sub

you cannot always use Datetime.now because it will only add 1 day to the current date. you need to parse the date from txtDate each time
That's simply excellent. Many thanks.
 
Upvote 0
Top