|
Post by alincon on Jun 29, 2021 20:15:25 GMT -5
Why doesn't this code work correctly?
r.m.
cnty$ = right$(date$(today$),2)
cnty$ = str$(val(cnty$) + 1)
print dateChek("062321")
wait
function dateChek(byref dat$)
if len(dat$) < 7 then dat$ = mdcySlash$(dat$)
dateChek = date$(dat$)
if dateChek = 0 then notice "Invalid Date" + chr$(13) + dat$
dat$ = str$(dateChek)
end function
function mdcySlash$(a$) 'mon day century year
mdcySlash$=left$(a$,2) + "/" + mid$(a$,3,2) + "/" + str$(19 + (right$(a$,2) < cnty$)) + right$(a$,2)
end function
|
|
|
Post by Brandon Parker on Jun 29, 2021 21:14:18 GMT -5
Well, you have hard coded "19" for starters... It's currently 2021... Don't just assume Millennium and Century; that's what caused the Y2K bug. What exactly are you attempting to do? Check whether a date is formatted correctly? Here is a quick example of what I think you might be trying to do. Let me know if I have gotten the idea wrong. I did not use "Notice" inside the function, but it is easily changeable. Note that the function requires a 4 digit year in the input, but it set up to output the year format as you wish in either 2 or 4 year format; all other year formats are invalid. Month and day places can be either 1 or two digits 'This should return the date since it is a correctly formatted date Print validateDate$("02/29/2020", 4)
'This should return the date since it is a correctly formatted date, but with only a 2 digit year Print validateDate$("02/29/2020", 2)
'This should return "Invalid year format requested! {yearFormat}" since it is a correctly formatted date, but 'we requested an invalid date format Print validateDate$("02/29/2020", 8)
'This should return "Invalid Date or Date Format! {month/day/year}" since 2021 is not a leap year and thus did not have a February 29. Print validateDate$("02/29/2021", 4) Wait
Function validateDate$(date$, yearFormat) 'date$ - The date in string format ' Expects date$ in the form of mm/dd/yyyy 'yearFormat - The number of placeholders for year you want ' the function to return If (yearFormat <> 2) And (yearFormat <> 4) Then validateDate$ = "Invalid year format requested! {";yearFormat;"}" : Exit Function validateDate$ = "Invalid Date or Date Format!" date$ = Trim$(date$) month$ = Word$(date$, 1, "/") day$ = Word$(date$, 2, "/") year$ = Right$(date$, 4) validateDate$ = validateDate$ + " {";month$;"/";day$;"/";year$;"}" If Date$(month$;"/";day$;"/";year$) Then validateDate$ = month$;"/";day$;"/";Right$(date$, yearFormat) End Function {:0) Brandon Parker
|
|
|
Post by alincon on Jun 30, 2021 9:08:00 GMT -5
"19" will work until 2099, and that's plenty long enough for me
I'm just trying to extend a date from 6 characters to 8 (see subject line) So "062321" should become "06232021", not "06231921", so I think the problem is in function mdcySlash$(a$)
I expected this code (right$(a$,2) < cnty$) to evaluate to 1, which added to 19 s/b 20, but that's not what happens.
r.m.
|
|
|
Post by tsh73 on Jun 30, 2021 14:01:52 GMT -5
I expected this code (right$(a$,2) < cnty$) to evaluate to 1, which added to 19 s/b 20, but that's not what happens. r.m. Your function function mdcySlash$(a$) does not see cnty$ variable. If you make it global, it starts working.
|
|
|
Post by alincon on Jun 30, 2021 19:10:57 GMT -5
Thank you for seeing the obvious thing that I couldn't see. r.m.
|
|
|
Post by Carl Gundel on Jun 30, 2021 20:16:35 GMT -5
Thank you for seeing the obvious thing that I couldn't see. r.m. Sounds like a useful check for compiler reporting.
|
|