iOS Question Get future date

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

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
 
Solution
B4X:
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

John Naylor

Active Member
Licensed User
Longtime User
Definitely should be using long.

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.
 
Last edited:
Upvote 0

John Naylor

Active Member
Licensed User
Longtime User
Use datetime.add


B4X:
Dim FutureDate As Long
FutureDate = DateTime.Add(DateTime.Now, 0, 0, 10)
Log("Future date is: " & DateTime.Date(FutureDate))
This of course works but it doesn't change the result if DateTime.DateFormat = "EEEE DD MMM YYYY"
 
Upvote 0

mcqueccu

Well-Known Member
Licensed User
Longtime User
B4X:
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
 
Upvote 0
Solution

John Naylor

Active Member
Licensed User
Longtime User
That's more like it! So it appears the date format has to be set differently at the start or the final result will be wrong.
 
Upvote 0

John Naylor

Active Member
Licensed User
Longtime User
DD should be lowercase dd in the format
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
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
B4X:
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
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Thanks heaps for everyones help on this! Something so simple but sometimes the simple thing gets us.

Looks like the code in post 5 worked.

I didn't notice the datetime.add(...) when I was trying to do this, but at least I now know for the future as well.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…