I am not sure if my code below is correct or not. I am assuming it's wrong and I think I shouldn't be using a int and need to use long, but when I change it to long it also doesn't work.
I am trying to get the date in xx days based on the current date. In the example below I tried to get the date in 10 days.
I am not 100% sure on how to add 10 days to the current date.
Any ideas on what I have done wrong, or anyone know how to do this ?
B4X:
DateTime.DateFormat = "EEEE DD MMM YYYY"
Dim extra_days As Int
extra_days = DateTime.TicksPerDay * 10 ' ticks per day x 10 days
extra_days = extra_days + DateTime.Now
Log(extra_days) ' logs 1675764148578
Log(DateTime.Date(extra_days)) ' Logs Tuesday 38 Feb 2023
DateTime.DateFormat = "dd-MM-yyyy"
Dim FutureDate As Long
FutureDate = DateTime.Add(DateTime.Now, 0, 0, 10)
DateTime.DateFormat = "EEEE dd MMM YYYY"
Log("Future date is: " & DateTime.Date(FutureDate)) '<----: Future date is: Tuesday 07 Feb 2023
I'm seeing the same error until I change the date format to dd-MM-yyyy
B4X:
DateTime.DateFormat = "dd-MM-yyyy"
Dim extra_days As Long
extra_days = DateTime.TicksPerDay * 10 ' ticks per day x 10 days
extra_days = extra_days + DateTime.Now
Log(extra_days) ' logs 1675764148578
Log(DateTime.Date(extra_days)) ' Logs 07-02-2023 - Correct
DateTime.DateFormat = "EEEE DD MMM YYYY"
Log(DateTime.Date(extra_days)) 'logs Tuesday 38 Feb 2023 - Wrong
Surely this is a bug.
[Edit] Also be wary of adding days onto datetime.now - 10 days from datetime.now where the time is 11am won't be 10 days on the morning of 7/2/2023 until the time gets past 11.00
I usually strip the time out of all such calculations just in case, unless that's the result you are going for of course.
DateTime.DateFormat = "dd-MM-yyyy"
Dim FutureDate As Long
FutureDate = DateTime.Add(DateTime.Now, 0, 0, 10)
DateTime.DateFormat = "EEEE dd MMM YYYY"
Log("Future date is: " & DateTime.Date(FutureDate)) '<----: Future date is: Tuesday 07 Feb 2023
DateTime.DateFormat = "dd-MM-yyyy"
Dim FutureDate As Long
FutureDate = DateTime.Add(DateTime.Now, 0, 0, 10)
DateTime.DateFormat = "EEEE dd MMM YYYY"
Log("Future date is: " & DateTime.Date(FutureDate)) '<----: Future date is: Tuesday 07 Feb 2023
Absolutely - to get it to work. However I would still argue that given EEEE DD MMM YYYY is a valid value for dateformat that it really shouldn't matter what is used it should never give a date of the 38th
public Sub DateAdd(NumberOfDays As Int,NumberOfMonths As Int,NumberOfYears As Int,DateFormat As String) As String
Try
Dim NewTick As Long
Dim Tick As Long
Dim str As String
Dim P As Period
Tick=DateTime.now
P.Days=NumberOfDays
P.Months=NumberOfMonths
P.Years=NumberOfYears
NewTick=DateUtils.AddPeriod(Tick,P)
str= DateTime.Date(NewTick)
Return str
Catch
Log("DateAdd" & LastException.Message)
Return ""
End Try
End Sub