Android Question Calculation of expiration date between two dates with expiration in hours

roby128128

Member
Hello I should calculate the expiration between two dates in hours, but the datetime.now value is always less than the expiration value, what am I doing wrong?. Thanks in advance to all of you. this is the code I wrote:

' now date 2021/09/30

DateTime.DateFormat = "yyyy/MM/dd"
Dim sDate As String = "2021/09/10" 'String Date
Dim lngDate As Long = DateTime.DateParse(sDate) 'Convert String to Long with Date Parse long =1631224800000
Log("mydate: "&DateTime.Date(lngDate))
Dim MyDate As String=DateTime.Date(DateTime.Now)

Dim p As Period
' I add 4 days and three hours = 99 hours
p.Hours=99
Dim t2 As Long = DateUtils.AddPeriod(lngDate, p) ' Expiration date 1631581200000
Log("scadenza: "&DateTime.Date(t2))
Log("Expiration date = " & DateTime.Now)
Dim diff = DateUtils.PeriodBetweenInDays(StripTime(t2),StripTime(DateTime.Now)).Days

If DateTime.Now > t2 Then
Log(DateTime.Now) ' 1631519334630
Log("expired")

Else
Log(DateTime.Now) ' 1631519334630
Log("active")
End If
 

roby128128

Member
Thanks Erel and sorry for the mistake

B4X:
' now date 2021/09/30 for example

DateTime.DateFormat = "yyyy/MM/dd"
Dim sDate As String = "2021/09/10" 'String Date
Dim lngDate As Long = DateTime.DateParse(sDate) 'Convert String to Long with Date Parse long =1631224800000
Log("mydate: "&DateTime.Date(lngDate))
Dim MyDate As String=DateTime.Date(DateTime.Now)

Dim p As Period
' I add 4 days and three hours = 99 hours
p.Hours=99
Dim t2 As Long = DateUtils.AddPeriod(lngDate, p) ' Expiration date 1631581200000
Log("scadenza: "&DateTime.Date(t2))
Log("Expiration date = " & DateTime.Now)
Dim diff = DateUtils.PeriodBetweenInDays(StripTime(t2),StripTime(DateTime.Now)).Days

If DateTime.Now > t2 Then
Log(DateTime.Now) ' 1631519334630
Log("expired")

Else
Log(DateTime.Now) ' 1631519334630
Log("active")
End If
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
but the datetime.now value is always less than the expiration value,
Here is what I think your code should look like although, you show variables that are not defined, a function striptime also not defined. If this does not help you, then you need to be more clear with your code.
You need to involve the TIME component in your dates since you are adding hours. Take a look at this solution and let us go from there:
B4X:
DateTime.DateFormat = "yyyy/MM/dd  HH:mm"
    Dim sDate As String = "2021/09/10  08:00"
    Dim lngDate As Long = DateTime.DateParse(sDate) 
'    Log("mydate: "&DateTime.Date(lngDate))

    Dim p As Period
    'I add 4 days and three hours = 99 hours
    p.Hours=99
    Dim t2 As Long = DateUtils.AddPeriod(lngDate, p)
    Log("scadenza: "& DateTime.Date(t2))
    If DateTime.Now > t2 Then
        Log("expired")
    Else
        Log("still active")
    End If
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

roby128128

Member
Thanks Mahares I tried with your code. the expiration date is 2021/09/14 I have brought the date of the pc to 2021/10/30 but it does not work it is always active and not expired, and datetime.now is higher than T2 expiration t2 =1631610000000 datetime.now =1631535888512 even lower than fdi t2 even if dated 2021/10/30
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
and sorry
Here it is in a form of a function , you can check expiration with different date scenarios:
B4X:
    Log(CheckExpiration("2021/09/07  00:00" , 99) )
    Log(CheckExpiration("2021/09/10  00:00" , 99) )
    Log(CheckExpiration("2021/09/13  15:00" , 11) )
    Log(CheckExpiration("2021/09/13  00:00" , 24) )


Sub CheckExpiration(sDate As String, inXhours As Double) As String
    DateTime.DateFormat = "yyyy/MM/dd  HH:mm"
    Dim lngDate As Long = DateTime.DateParse(sDate)
    Dim p As Period
    p.Hours= inXhours
    Dim t2 As Long = DateUtils.AddPeriod(lngDate, p)
    Log("scadenza: "& DateTime.Date(t2))
    Return IIf(DateTime.Now> t2, "expired", "still active")
End Sub
 
Upvote 0

roby128128

Member
Thank you very much and excuse me, I was wrong in practice I sent forward the date of the PC and not that of the phone that I use as a bridge, both the proposed tea solutions work even if the second and more fluid. Thanks again you solved me a problem. I think it also worked the part of my code, except the striptime put by mistake in my example, Thanks so much greetings Roberto
 
Upvote 0
Top