Android Question current date + 100 days error !!

krlos2004

Member
Licensed User
Longtime User
Hello

must calculate that day will be current date + 100 days

I tried so but this in error, can help me?

DateTime.SetTimeZone(0)
DateTime.DateFormat="dd/mm/yyyy"

Dim Day As Int = 100, ETicks1 As Long

ETicks1 = DateTime.DateParse( "09/03/2016" )

Msgbox( DateTime.Date(ETicks1+(Day * 86400000)), "ok" )


Return 09/57/2016 ????!!!
 

wonder

Expert
Licensed User
Longtime User
B4X:
Sub Days_to_Milliseconds(days As Int) As Long
    '24 * 60 * 60 * 1000 = 86400000
    Return (days * 86400000)
End Sub

Log(DateTime.Date(DateTime.Now + Days_to_Milliseconds(100))

...but both solutions above are way more straight forward. :)
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
B4X:
DateTime.DateFormat="MM/dd/yyyy"
Dim d1 As Long = DateTime.DateParse( "01/10/2015" )
Msgbox(DateTime.Date(DateTime.Add(d1,0,0,200)), "newdate" )

Result : 07/29/2015

B4X:
DateTime.DateFormat="dd/MM/yyyy"
Dim d1 As Long = DateTime.DateParse( "10/01/2015" )
Msgbox(DateTime.Date(DateTime.Add(d1,0,0,200)), "newdate" )

Result : 29/07/2015

B4X:
DateTime.DateFormat="dd/MM/yyyy"
Dim d1 As Long = DateTime.DateParse( "01/10/2015" )
Msgbox(DateTime.Date(DateTime.Add(d1,0,0,200)), "newdate" )

Result : 18/04/2016

Angry ?
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Knowing @Erel as a big fan of DateUtils, I thought maybe I add this solution to the good solutions listed in the previous posts above:
B4X:
Dim p As Period
p.Days = 100
Dim nextdate As Long = DateUtils.AddPeriod(DateTime.DateParse("09/03/2016"), p)  'adds 100 days to this date
Log($"next date: $Date{nextdate}"$)  'or Log("Next date: " & DateTime.Date(nextdate))
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Why dont you use the TicksPerDay

Dim hundreddays As Long = DateTime.TicksPerDay * 100
Dim DateInHundredDays As Long = DateTime.Date(DateTime.Now + hundreddays)
The correct way is to use DateUtils.AddPeriod. DateTime.Add is also correct in this case.

The above code is incorrect and will not handle DST properly.
 
Upvote 0
Top