Problem with increase date

Hi group, another problem with dates that I can’t figure out … I’ve tried all kinds of conversions but I can’t increase the value of a date by X days. I’ve been reading and I’m confused even more. I’ve read of people who increase the days with seconds … I thought something like this could be done. Data+5 passes the date to the 5th day following, 34 days passes to the following month and so on …
I also have problems with the formatted date … I use .short dates, but let’s say that I can convert a short date into a long one like YYYY/mm/dd … The problem is that I can’t add a number to a date X of days.


Dim EvaluateDate as string   'initial date
Dim DeadLineDate  as date   'Evaluate date
Dim Days  as integer   'day to increase initial date
Days=integer.FromString(TextField6.Text)
EvaluateDate=TextField2.Text    'short date   Ex: 10/10/2022

i would like to do something like this

DeadLineDate=EvaluateDate+ days

I tried to read DateTime — Xojo documentation but nothing

Do you want to use Date (deprecated) or DateTime?

If DateTime, you may want to read about DateInterval and the different constructors so you can create a new DateTime with the desired date.


In short, you need to convert your String Short Date to a DateTime, then you can use DateInerval to add 5 days or create a new DateTime (DeadLineDate) by adding seconds to the constructor.

i’m use DATE… i was trying with a series of conversions… i think i’m missing out… i thought it was easier.

Dim EvaluateDate as string   'data iniziale
Dim DeadLineDate  as date   'scadenza
Dim Days  as integer
Days=integer.FromString(TextField6.Text)



EvaluateDate=TextField2.Text    'short date
'messagebox EvaluateDate.ShortDate


// THIS FUNCTION CONVERTS THE SQLDATE INPUT TO THE Y/M/D FORMAT
Dim convertedDate as String
Dim fieldY, fieldD, fieldM as String
fieldD = EvaluateDate.NthField("/",1)
messagebox "Day=" + fieldD.ToText
fieldM = EvaluateDate.NthField("/",2)
messagebox " Mese=" +fieldM.ToText
fieldY = EvaluateDate.NthField("/",3)
messagebox "Year=" +fieldY.ToText
// REASSEMBLE DATE INTO THE APPROPRIATE UK FORMAT
convertedDate = fieldY+"-"+fieldM +"-"+fieldD
messagebox convertedDate


Var SQLDate As String = convertedDate
Var myDate2 As DateTime = DateTime.FromString(SQLDate)


MsgBox "??????????" + myDate2.ToString

I would like to understand how to increase the date by TOT days and if by increasing the days to the date, the month or year also automatically changes.

There are a few problems with your code:

  • you are using Date for DeadLineDate
  • you are using DateTime for myDate2
  • your conversion does not handle dates correctly to SQLDate, for example fieldY can be 23 and SQLDate needs that to be 2023, fieldD can be 4 and SQLDate needs that to be 04

You need to read and practice a little more. Use the following code to practice/test:

dim d as new date
d.Year = 2023
d.Month = 1
d.Day = 4
dim test1 as string = d.SQLDate //2023-01-04
d.Day = d.Day + 5
dim test2 as string = d.SQLDate //2023-01-09
Break

Ok guys, always precious with your suggestions. So here’s my listing, it seems to work. Do you have any suggestions to improve it?
(I have a date read as a string, convert it to an integer, add the days I want to the date, get the new date) I organized everything with a function.

Function ManipolaData(DataPassata as string, SommaGiorni as integer) return string

Dim AnnoPassato as integer
Dim MesePassato as integer
Dim GiornoPassato as integer
Dim ComposizioneData() as string=Split(DataPassata.ReplaceAll(" ",""),"/")
GiornoPassato=ComposizioneData(0).ToInteger
MesePassato=ComposizioneData(1).ToInteger
AnnoPassato=ComposizioneData(2).ToInteger

messagebox " D*M*Y extract : " + GiornoPassato.ToText+ " ** " + MesePassato.ToText+ " ** "  + AnnoPassato.ToText

dim d as new date
d.Year = Annopassato
d.Month = MesePassato
d.Day = GiornoPassato

dim test1 as string = d.SQLDate   'Date extrapolation

d.Day = d.Day + SommaGiorni
dim test2 as string = d.SQLDate     'SUm day at my Date

return test2

and in the main:

Dim LaMiaData as datetime
LaMiaData=datetime.FromString(ManipolaData(“06/10/1979”,2))

messagebox "My manipulated date >>>> " + LaMiaData.ToString(DateTime.FormatStyles.Short, DateTime.FormatStyles.None)

Hi Fede, how are you? Hope you’re fine…
I solved your problem with an extension method that extends a date class (API 1.0, Xojo 2019r1.1).

Add(Extends myDate As Date, NumberToAdd As Integer, sigla As String = “d”)
// Somma ad una data il valore . Questo valore è specificato da :
// a) “h” = ore
// b) “d” (default) = giorni
// c) “m” = mesi
// d) “y” = anni
Const SecondiMinuto = 60
Const SecondiOra = SecondiMinuto * 60
Const SecondiGiorno = SecondiOra * 24
Select Case sigla
// ore
Case “h”
myDate.TotalSeconds = myDate.TotalSeconds + (SecondiOra * NumberToAdd)
// giorni
Case “d”
myDate.TotalSeconds = myDate.TotalSeconds + (SecondiGiorno * NumberToAdd)
// mesi
Case “m”
myDate.Month = myDate.Month + NumberToAdd
// anni
Case “y”
myDate.Year = myDate.Year + NumberToAdd
End Select

Hi Nedi all ok. I go on with learning Xojo, I have achieved many things and I can be happy. Of course, I still have to learn so much… I will try your suggestion as soon as possible

Grazie Nedi, sempre gentile :slight_smile: