DATE - read years YY in YYYY

Hi group, a problem with dates … I’m building a function … if I write a date like DD/MM/YYYY everything works, but if I pass the value DD/MM/YY I get random values ​​… I read the date of type string with

Dim PastYear as integer
Dim PastMonth as integer
Dim PastDay as integer
Dim CompositionData() as string=Split(DataPassata.ReplaceAll(" ",""),"/")
PastDay=CompositionDate(0).ToInteger
Month Past=CompositionDate(1).ToInteger
YearPast=CompositionDate(2).ToInteger ...

I think the problem lies in how I read the year. How can I read the correct year even in two digits?
I need to read the year YY as YYYY

Your code use Dim CompositionData but uses CompositionDate
Your code use Dim PastMonth but use Month Past
Your code use Dim PastYear and use YearPast
after fixing those errors, there is no problem getting YY or YYYY, so I guess that your problem is somewhere else.

Edit: From other post I think you are using Date, take a look at ParseDate.

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

when i press a button: messagebox “>>” + ManipolaData(TextField1.Text,0)

If i passed date 06/10/1979 extract d=6 m=10 Y=1979 but if i passed 06/10/79 extract 06/01/1601… WHY ?

Are you using Windows?

https://documentation.xojo.com/api/deprecated/date.html#notes

image

yes I use qindows, all the examples I’ve read speak of dates with a 4-digit year of the type YYYY. Even if I use 4 digits for the year, everything works, but if I read a date like dd/mm/YY, everything comes out wrong.

You just need to check if you have 2 digits and add the 2 missing digits to make it YYYY.

If you only put 2 digits for the year, you are trying to set the date as 06/10/0079, that’s 1900 years before than expected and with Windows it can’t go below 1601, that’s why you get weird dates.

....
AnnoPassato=ComposizioneData(2).ToInteger

if annopassato<=100 then
  annopassato=annopassato+1900
end if
....

so ?

Now it works, no?

it still doesn’t work very well.
with this if if I read 06/10/79 I get 06/10/1979 and I solved a problem … but if I read the date 06/10/22 I get 06/10/1922 … how do I bring out 06/10/2022 ?

1 Like

Well, you need to put some logic on your code.
Which years from 00 to 99 will be 19xx?
Which years from 00 to 99 will be 20xx?
Are you going to change that each year depending if you are in 2022, 2023, …?

if annopassato<=100 then
annopassato=annopassato+1900
if annopassato<1979 then
annopassato=annopassato+100
end if
end if

so it seems to me that it works until 2078… i’m trying to figure out how to get the correct year when it’s formatted in 2 digits.

Make your inputs accept 2 formats, YY = 20YY (you can automate to get the century part from the current year, that 20) and YYYY is the full date. So when wanting 79 as 1979, write 1979 instead of 79.

(Okay, let’s say in reading the year I should have solved) Instead,
to solve the problem of entering the year in the correct YYYY format in the textfield can I set the Validation Mask as ##/##/#### ?? and put an if ####<=99 then say to indicate the complete year with 4 digits ?

P.S: is there the possibility of having the characters __ / __ / ____ appear in the textfield in order to make the user understand in advance that the format must be of the type dd / mm / YYYY ?

Use ^ that, or you need to write your own input handling with all the options you want, try using the “hint” feature, like “dd/mm/yyyy” there

The way I handle it is to use a rolling 15 year window into the future. So if today is 1/1/23 and they put in 1/1/38, it’s 2038. If they enter 1/1/39, it’s 1939. Most of the ambiguity is within the last 50 years, so dates like 1/1/82 are expected to be 1982 and 1/1/05 is expected to be 2005 and 1/1/24 should be 2024. I also allow them to enter the entire 4 digit year, of course.

Perfect !

1 Like

Thanks everyone for the valuable suggestions

Using the date picker would also solve this problem.

3 Likes