I have the datetime issues with old code, i am sti...
# cfml-general
g
I have the datetime issues with old code, i am still getting invalid date, month combination https://trycf.com/gist/bed376017d2ebe2b89be3c3073e29929/acf2021?theme=monokai
a
Am quite keen to hear about this 29th month you are using in that example...
g
same thing Adam, not my code, legacy legacy
a
Well what are you expecting? There's no 29th month. There are only 12 months. What other answer are you looking for here?
m
you are using mm/dd/yyyy in
SAPPDateFormat
, but seemingly reversing the mm & dd
g
i think i am sharing the old code is behaving very odd, so i was checking if i could write it a better way instead of cutting slicing
a
THEMONTH=	'29';
☝️ 2
t
The part of this code I find most confusing is that mth_idx is 12 in both cases... in the first case (sAppDateFormat is the European date format), EndDt is "12/29/2023" (an invalid European date, representing the 12th of 29-ember.) but mth_idx is 12. This makes sense because, per the docs, dateFormat assumes US date formats. In the second case (sAppDateFormat is not European), EndDt is "29/12/2023" (an invalid US date, representing the 12th of 29-ember) But mth_idx is still 12.... I'd have expected it to throw an error there because it's not a valid US date.
g
i found the root cause in the code before the code i shared, they have used this: <cfset MyDate=ArrayNew(1)> <cfset MyDate=#ListToArray(StartDt,"/")#> <cfset TheDay=MyDate[1]> <cfset TheMonth=MyDate[2]> <cfset TheYear=MyDate[3]> and that is ending in month as 29, quite weird, seeing if i can find a better way to deal with it
s
If you swap the values of
THEDAY
and
THEMONTH
so they are valid you get
{ts '2023-12-29 00:00:00'} {ts '2024-12-28 00:00:00'}
as output. As it stands, you get an error because
THEMONTH
is
29
. If you change the format to
SAPPDATEFORMAT=	'dd/mm/yy';
(not
yyyy
because the
if
checks for
yy
) you get an error unless you swap day/month values -- again because month cannot be 29!
a
Elided version of your two bits of code with all the cruft removed
Copy code
<cfscript>
STARTDT	='12/29/2023';
</cfscript>

<cfset MyDate=#ListToArray(StartDt,"/")#>
<cfset TheDay=MyDate[1]>
<cfset TheMonth=MyDate[2]>
and that is ending in month as 29, quite weird
In what way is it weird? You're treating the second part of that date as the month, when clearly it's the day of the month.
STARTDT
is in format
mm/dd/yyyy
and your code is pretending it's
dd/mm/yyyy
, and then... well... that's never gonna work as you found out. What are you actually trying to achieve here? Forget about code. Articulate it in English
m
imo, (and it's based on what I assume you are trying to do), you should be using parsedatetime or lsparsedatetime and not manipulate the strings yourself when you are trying to get dates. https://trycf.com/gist/73b18f1cc2bef5203452a35db57df8b9/acf2021?theme=monokai
💯 3
g
I just wanted to chime in here - with an observation - just in case it wasn't clear for you. For the code that you provided in the gist - as the reader of your question / code we assume that the code you provided is exactly what is written in your application... And as a result of that we can tell you that the code is "just wrong". Swapping the the variables THEDAY and THEMONTH will wholly fix your issue in this instance - as others have stated already. It is really obvious - straight away what the issue is. And because we can only go off what you have written in your post - we cannot give you an answer that will work for you, if this is the case. Based ONLY what you have provided to us the only sensible solution to your post is to tell you that you need to swap the variables
THEDAY
and
THEMONTH
Copy code
<cfscript>
THEDAY = '29';
THEMONTH = '12';
THEYEAR = '2023';
STARTDT	='12/29/2023';

SAPPDATEFORMAT = 'MM/dd/yyyy';
</cfscript>
But again - this assumes that they code in your gist is EXACTLY the same as what is running in your application. If it is different in any way - then everyone's advice to you, isn't going to be very helpful. Let me explain. IF the real code in your application actually gets the values from a form - or from a database and THEN populates
THEDAY
and
THEMONTH
/ etc - then - the issue is something completely different. And I would "guess" (and I can only guess - because of what you have written) - that if that is the case - the issue lies in the way you're getting your date time data in the first place or how you're going about putting the individual parts into those (
THEDAY
,
THEMONTH
,
THEYEAR
) variables. Lastly about date / time MASKS. And while it seems that you have the date mask correct in your example - make sure, please, that you realise that for a mask - the date / time parts are Case-Sensitive. Previously - (I think) in older versions - CFML used to do a whole lot of extra work and try and work out what you meant with your masks - irrespective of the case you chose. This caught me out a couple of years ago - in that we had really old code that had incorrect casing for the time-parts and now we were noticing there were issues. We were seeing the MONTH where there should be MINUTES. So it was either we were just really bad at noticing before - OR the CFML engine handled the masking differently in previous versions and perhaps tried to "Guess" what you meant - irrespective of the CASE you used So if you wrote;
dateTimeFormat(now(), "DD/MM/YYYY HH:MM:SS")
It would work out for you that although you wrote "MM" in the time segment - you really meant to write "nn" and would correctly display your minutes 00-59. We noticed it - when we all of our MINUTES never got above 12! Anyway - if you need them: Here are the docs that give you the details about the masks you can use. Adobe-dateTimeFormat Lucee-dateTiemFormat
💯 1
Oh Also - just another thought.... In your example if the start and end dates are being used to define a range, The
EndDt
will miss everything that happened on the last day - the day STARTS at 000000 and ENDS at 235959 That is you "MIGHT" need to use the time, also and end up with StartDt2 : YYYY-MM-DD 000000 EndDt : YYYY-MM-DD 235959 At the moment they both have 000000 - in your example.