Android Question Time subtractions

sot bel

Member
Hello …Happy new year … My best wishes to all of you !!!

I want to do some time subtractions but I don’t know how...

I have the time in format 2021-01-03 02:15:24 and i want to subtract
-30 minutes

-12 hours

-1 day

-1 week

-1 month

-1 year

The result must be in the same format 2020-01-01 18:33:11
 

josejad

Expert
Licensed User
Longtime User

Use DateUtils, and DateTime

1609746014185.png
 
Upvote 0

sot bel

Member
Solved by using dateutils..
Thank you ...



B4X:
Sub Thing_results(TTT As Int)
    Dim TFrom As Long
    Dim TTo As Long
    Dim DT As String
    DateTime.DateFormat ="yyyy-MM-dd HH:mm:ss"
    TTo=DateTime.Now
    Select Case TTT
        Case 1
            TFrom=TTo-(DateTime.TicksPerday/2)    '12h
        Case 2
            TFrom=TTo-(DateTime.TicksPerday)    '1 day
        Case 3
            TFrom=TTo-(DateTime.TicksPerday*7)    '1 week
        Case 4
            TFrom=TTo-(DateTime.TicksPerDay*30)    '1 month
        Case 5
            TFrom=TTo-(DateTime.TicksPerDay*365)    '1 year
        Case 6
    
    End Select
    DT="start="&DateTime.Date(TFrom)&"&end="&DateTime.Date(TTo)
    Return DT

End Sub
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Sorry but this is the wrong way to do date calculations.

As @Erel says watch the Date & Time Tutorial. You should be using the Period object

the correct code is:

B4X:
Sub Thing_results(TTT As Int) As String
    Private per As Period
    
    per.Initialize
    
    Dim TFrom As Long
    Dim TTo As Long
    Dim DT As String
    DateTime.DateFormat ="yyyy-MM-dd HH:mm:ss"
    TTo=DateTime.Now
    Select Case TTT
        Case 1
            per.hours = -12
            'TFrom=TTo-(DateTime.TicksPerday/2)    '12h
        Case 2
            per.Days = -1
            'TFrom=TTo-(DateTime.TicksPerday)    '1 day
        Case 3
            per.Days = -7
            'TFrom=TTo-(DateTime.TicksPerday*7)    '1 week
        Case 4
            per.Months = -1
            'TFrom=TTo-(DateTime.TicksPerDay*30)    '1 month
        Case 5
            per.Years = -1
            'TFrom=TTo-(DateTime.TicksPerDay*365)    '1 year
        Case 6
    
    End Select
    TFrom = DateUtils.AddPeriod(TTo,per)
    
    
    DT="start="&DateTime.Date(TFrom)&"&end="&DateTime.Date(TTo)
    Return DT

End Sub

The reason for this is because of leap years, months having days <> 30 etc. AddPeriod handles all of that for you.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Solved by using dateutils..
I really don't think the way you did it is the correct way (a month is not always 30 days), but here is a sub that will do what you want and you can add or subtract minutes, days, months, years and mix any combination of period:
B4X:
'Calculate a string date with time after adding or subtracting a period
Sub MyDate(dstart As String, tm As Double, th As Double, td As Double, tmo As Double, ty As Double) As String
    DateTime.DateFormat ="yyyy-MM-dd HH:mm:ss"
'    Dim dstart As String ="2021-01-03 02:15:24"
    Dim p As Period
    p.Minutes=tm
    p.Hours=th
    p.Days=td
    p.Months=tmo
    p.Years=ty
    Return DateTime.Date(DateUtils.AddPeriod(DateTime.DateParse(dstart), p))
End Sub
To test:
B4X:
Log(MyDate("2021-01-03 02:15:24", 0, 0, 0, 2, 0))     'logs: 2021-03-03 02:15:24     we added 2 months
    Log(MyDate("2021-01-03 02:15:24", -30, 0, 0, 0, 0))   'logs: 2021-01-03 01:45:24.  we subtracted 30 minutes
    Log(MyDate("2021-01-03 02:15:24", -1, -1, -1, -1, -1))   'logs: 2019-12-02 01:14:24
 
Upvote 0

sot bel

Member
I wanted to fill an xchart with data .. The accuracy of time is not necessary.
Finally i use the code of Andrew (Digitwell).
Thank you all for your answers..



en.jpg
 
Last edited:
Upvote 0
Top