iOS Question DateUtils

Balasubramanian

Member
Licensed User
Longtime User
How to add 405 days with the help of DateUtils or any other

Ex : 06-23-2025 + 405 days
 

Attachments

  • Screenshot 2025-06-23 at 18.08.53.png
    15.5 KB · Views: 51

aminoacid

Active Member
Licensed User
Longtime User
How to add 405 days with the help of DateUtils or any other

Ex : 06-23-2025 + 405 days

Just for kicks, I asked ChatGPT your question in this form -

Write a B4X sub that takes a date in the form "MM/DD/YYYY" and a day offset (in days), adds the number of days in the day offset to the given date and return the resulting date as a string.

Here is what he/she came up with -

B4X:
Sub AddDaysToDate(InputDate As String, DayOffset As Int) As String
    Dim sdf As B4XSerializator ' Just to support cross-platform compatibility
    Dim DateFormat As String = "MM/dd/yyyy"
   
    Dim DateFormatter As B4XFormatter
    DateFormatter.Initialize
    DateFormatter.DateFormat = DateFormat
   
    Dim d As Long
    Try
        d = DateTime.DateParse(InputDate)
    Catch
        ' Fallback if DateParse does not understand the format
        Dim parts() As String = Regex.Split("/", InputDate)
        If parts.Length <> 3 Then Return "Invalid date format"
        Dim month As Int = parts(0)
        Dim day As Int = parts(1)
        Dim year As Int = parts(2)
        d = DateUtils.SetDate(year, month, day)
    End Try

    Dim newDate As Long = d + DayOffset * DateTime.TicksPerDay
    Return DateTime.Date(newDate)
End Sub

Usage Example:

B4X:
Dim newDate As String = AddDaysToDate("06/23/2025", 10)
Log(newDate)  ' Output: 07/03/2025



Have not tried it. I'll let you do that.

Let us know how it works.
 
Last edited:
Upvote 0

aminoacid

Active Member
Licensed User
Longtime User
The first 5 statements are not necessary. Here is the revised sub:

B4X:
Sub AddDaysToDate(InputDate As String, DayOffset As Int) As String
 
    Dim d As Long
    Try
        d = DateTime.DateParse(InputDate)
    Catch
        ' Fallback if DateParse does not understand the format
        Dim parts() As String = Regex.Split("/", InputDate)
        If parts.Length <> 3 Then Return "Invalid date format"
        Dim month As Int = parts(0)
        Dim day As Int = parts(1)
        Dim year As Int = parts(2)
        d = DateUtils.SetDate(year, month, day)
    End Try

    Dim newDate As Long = d + DayOffset * DateTime.TicksPerDay
    Return DateTime.Date(newDate)
End Sub
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
B4X:
DateTime.DateFormat = "MM-dd-yyyy"
Dim Date1 As Long = DateTime.DateParse("06-23-2025")
Dim Period1 As Period
Period1.Days = 405
Dim Date2 As Long = DateUtils.AddPeriod(Date1, Period1)
Log(DateTime.Date(Date2))
 
Upvote 1

Balasubramanian

Member
Licensed User
Longtime User
Working Thank you
Working Thank you
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…