Android Question What is the right way to compare date in yyyy-MM-dd hh:mm format with system date time

beelze69

Active Member
Licensed User
Longtime User
Hi,

I am taking in a text input date in format yyyy-MM-dd hh:mm and compare it with current
system datetime to ascertain how many days, hours and minutes is the difference.

I attempted to convert both the system-system date and inputted date into
ticks before making the comparison.

I am attaching the relevant code..

This is not working..

The code and the error message is attached.

Please guide me in this regard.

Thanks.

B4X:
Private Sub btnCompareDate_Click
xui.MsgboxAsync("To compare the dates","B4X")
    Dim f As Boolean
    Dim inputDate As String
    Dim d As Long
    Dim t As Long
    inputDate=txtInputDate.Text.Trim
    'first set the date format to the format in which the date is inputted in the text control viz. in dd/mm/yyy hh:mm
    DateTime.DateFormat = "yyyy-MM-dd hh:mm"
    d = DateTime.DateParse(inputDate)
    t = DateTime.Now '
    Log("Input date and time :" & d)
    Log("Current Date and Time:" & t)
    Log("Your Selected Date is " & DateTime.Date(d) & " " & "Current Date is " & DateTime.Date(t))
    If DateTime.Date(d) = DateTime.Date(t) Then
        xui.MsgboxAsync("The date and time match !","B4X")
        
    Else
        If DateTime.Date(d)< DateTime.Date(t) Then
        xui.MsgboxAsync("You have YET :" & DateTime.Date(t-d),"B4X")
        Else
        xui.MsgboxAsync("It is already PAST:" & DateTime.Date(d-t),"B4X")
        End If
    End If
End Sub


It gies this error..


b4xmainpage_btncomparedate_click (java line: 65)
java.text.ParseException: Unparseable date: "2025-02-08 00:05"
    at java.text.DateFormat.parse(DateFormat.java:362)
    at anywheresoftware.b4a.keywords.DateTime.DateParse(DateTime.java:148)
    at b4a.example.b4xmainpage._btncomparedate_click(b4xmainpage.java:65)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:7189)
    at android.view.View.performClickInternal(View.java:7166)
    at android.view.View.access$3500(View.java:819)
    at android.view.View$PerformClick.run(View.java:27682)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:224)
    at android.app.ActivityThread.main(ActivityThread.java:7592)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
 

emexes

Expert
Licensed User
Longtime User
I feel that soon you'll be looking for something like:

B4X:
Sub WordsApart(Ticks As Long) As String
    If Ticks < 0 Then Ticks = -Ticks    'Long Abs
  
    Dim Apart As Long = Ticks / 1000
    Dim ApartUnits As String = "second"
  
    If Apart >= 60 Then
        Apart = Apart / 60
        ApartUnits = "minute"
      
        If Apart >= 60 Then
            Apart = Apart / 60
            ApartUnits = "hour"
          
            If Apart >= 24 Then
                Apart = Apart / 24
                ApartUnits = "day"
              
                If Apart >= 7 Then
                    Apart = Apart / 7
                    ApartUnits = "week"
                End If
            End If
        End If
    End If
  
    If Apart <> 1 Then ApartUnits = ApartUnits & "s"    'plural
  
    Return Apart & " " & ApartUnits
End Sub

that when tested with something like:

B4X:
Dim T As Long = 500    'less than one second, to demonstrate "0 seconds late"

Log("Due             " & TAB & "Returned        " & TAB & "Message")
Log("================" & TAB & "================" & TAB & "================")

Do While T < DateTime.TicksPerDay * 30
    Dim ReturnDate As Long = DateTime.Now
    Dim DueDate As Long = ReturnDate - T

    Dim HowLongOver As Long = ReturnDate - DueDate 

    If HowLongOver > 0 Then    'change to eg > DateTime.TicksPerMinute
        DateTime.TimeFormat = "dd/MM/yyyy HH:mm"
        Dim DueDateString As String = DateTime.Time(DueDate)
        Dim ReturnDateString As String = DateTime.Time(ReturnDate)
  
        Dim HowLongOverString As String = "Your book was " & WordsApart(HowLongOver) & " late"
  
        Log(DueDateString & TAB & ReturnDateString & TAB & HowLongOverString)
    End If
  
    T = T * 1.5
Loop

generates:

Log output:
Waiting for debugger to connect...
Program started.
Due                 Returned            Message
================    ================    ================
08/02/2025 11:32    08/02/2025 11:32    Your book was 0 seconds late
08/02/2025 11:32    08/02/2025 11:32    Your book was 0 seconds late
08/02/2025 11:32    08/02/2025 11:32    Your book was 1 second late
08/02/2025 11:32    08/02/2025 11:32    Your book was 1 second late
08/02/2025 11:32    08/02/2025 11:32    Your book was 2 seconds late
08/02/2025 11:32    08/02/2025 11:32    Your book was 3 seconds late
08/02/2025 11:32    08/02/2025 11:32    Your book was 5 seconds late
08/02/2025 11:32    08/02/2025 11:32    Your book was 8 seconds late
08/02/2025 11:32    08/02/2025 11:32    Your book was 12 seconds late
08/02/2025 11:32    08/02/2025 11:32    Your book was 19 seconds late
08/02/2025 11:32    08/02/2025 11:32    Your book was 28 seconds late
08/02/2025 11:31    08/02/2025 11:32    Your book was 43 seconds late
08/02/2025 11:31    08/02/2025 11:32    Your book was 1 minute late
08/02/2025 11:31    08/02/2025 11:32    Your book was 1 minute late
08/02/2025 11:30    08/02/2025 11:32    Your book was 2 minutes late
08/02/2025 11:28    08/02/2025 11:32    Your book was 3 minutes late
08/02/2025 11:27    08/02/2025 11:32    Your book was 5 minutes late
08/02/2025 11:24    08/02/2025 11:32    Your book was 8 minutes late
08/02/2025 11:20    08/02/2025 11:32    Your book was 12 minutes late
08/02/2025 11:14    08/02/2025 11:32    Your book was 18 minutes late
08/02/2025 11:04    08/02/2025 11:32    Your book was 27 minutes late
08/02/2025 10:51    08/02/2025 11:32    Your book was 41 minutes late
08/02/2025 10:30    08/02/2025 11:32    Your book was 1 hour late
08/02/2025 09:59    08/02/2025 11:32    Your book was 1 hour late
08/02/2025 09:12    08/02/2025 11:32    Your book was 2 hours late
08/02/2025 08:02    08/02/2025 11:32    Your book was 3 hours late
08/02/2025 06:17    08/02/2025 11:32    Your book was 5 hours late
08/02/2025 03:39    08/02/2025 11:32    Your book was 7 hours late
07/02/2025 23:42    08/02/2025 11:32    Your book was 11 hours late
07/02/2025 17:48    08/02/2025 11:32    Your book was 17 hours late
07/02/2025 08:55    08/02/2025 11:32    Your book was 1 day late
06/02/2025 19:37    08/02/2025 11:32    Your book was 1 day late
05/02/2025 23:39    08/02/2025 11:32    Your book was 2 days late
04/02/2025 17:43    08/02/2025 11:32    Your book was 3 days late
02/02/2025 20:48    08/02/2025 11:32    Your book was 5 days late
31/01/2025 01:26    08/02/2025 11:32    Your book was 1 week late
26/01/2025 20:22    08/02/2025 11:32    Your book was 1 week late
20/01/2025 12:47    08/02/2025 11:32    Your book was 2 weeks late
11/01/2025 01:25    08/02/2025 11:32    Your book was 4 weeks late
Program terminated (StartMessageLoop was not called).
 
Last edited:
Upvote 0

beelze69

Active Member
Licensed User
Longtime User
Hi,

I later figured out that DateUtils offers lot of useful features regarding such date and time comparisons.
B4X:
    Dim PerDiff As  Period
    Dim constructedInputDateTime As String 'this will hold the constructed date and time
    'let us store a date time
    constructedInputDateTime="2025-02-08 17:25"
    Dim constructedSystemDateTime As String 'a variable to hold the System Date and Time
    'setting the suitable format for the DateTime ..
    DateTime.DateFormat="yyyy-MM-dd HH:mm"
    constructedSystemDateTime=DateTime.Date(DateTime.Now)
    'a variable to hold the Ticks count for the System Date time
    Dim dSystemDateWithTime As Long
    'now store the Ticks count of the System time
    dSystemDateWithTime = DateTime.DateParse(constructedSystemDateTime)
    'a variable to hold the Ticks count for the Input Date Time
    Dim dInputDateWithTime As Long
    dInputDateWithTime=DateTime.DateParse(constructedInputDateTime)

    'now obtain the Period Difference in Days, Hours, Minutes and Seconds

    PerDiff=DateUtils.PeriodBetweenindays(dSystemDateWithTime,dInputDateWithTime)
    Log("Current Date and Time:" & constructedSystemDateTime)
    Log("Inputted Date and Time:" & constructedInputDateTime)
    Log("Difference in Days:" & PerDiff.days)
    Log("Difference in Hours:" & PerDiff.hours)
    Log("Difference in Minutes:" & PerDiff.minutes)
    Log("Difference in Seconds:" & PerDiff.seconds)

I wrote a small example and it works fine.

Any improvisation on the code will be very helpful.

Thanks.
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
Is there any particular reason for converting the current system ticks to a String and back again to ticks?

B4X:
constructedSystemDateTime = DateTime.Date(DateTime.Now)
dSystemDateWithTime = DateTime.DateParse(constructedSystemDateTime)
 
Upvote 0

beelze69

Active Member
Licensed User
Longtime User
Is there any particular reason for converting the current system ticks to a String and back again to ticks?

B4X:
constructedSystemDateTime = DateTime.Date(DateTime.Now)
dSystemDateWithTime = DateTime.DateParse(constructedSystemDateTime)
Hi emexes,

Well ! You are right .. That is not necessary exactly...
 
Upvote 0
Top