this is my test case, seems working well, but not ...
# lucee
g
this is my test case, seems working well, but not sure why in my code it is displaying the 1 instead of 01 https://trycf.com/gist/d9ff9aa9ef685b342883747d52bd581c/acf2021?theme=monokai
r
The DateFormat is only applying to the FromDate, and only if the FromDate doesn't exist, and not the StartDate. The code looks overly complicated. What are you trying to accomplish?
g
i am trying to extract the values using the mid and the createdate, i am not sure if it can be simplified but i am ok with it if it can be
m
mid is not a good way to work with dates. year() month() day() or dateFormat() would be better choices
also, you could use parseDateTime() over the createDate() in your scenario also
<cfdump var="#year( parseDateTime( startDate ) )#">
<cfdump var="#parseDateTime( startDate ).year()#">
g
changed my gist exactly as i have in code
can i have the code in your way you would write, its a legacy codeand i am not a fan of mid too, i rarely use it
r
There is no need to
<cfset StartDateIn = createdate(mid(FromDate,7,4),mid(FromDate,1,2),mid(FromDate,4,2))>
since it's already a date. You just need to
<cfdump var="#Year(FromDate)#>
.
Well, it's a string representation of a date and if you want an actual date object, use parseDateTime() as Matt suggested.
<cfset fromDate = lsParseDateTime(fromDate)>
<cfdump var="#fromDate#">
<cfdump var="#year(fromDate)#">
g
The default is not necessarily what you are using though. If the argument is passed in - it may well not be in dd/mm/yyyy format. Check your form / arguments / api - whatever it is that the cfparam is the "default" for / ensuring the source is validating the date/time in the expected format.
g
The form is passing the 1/01/2022 How can I fix when it reaches this page should I force the mask using whatever format it expects again so it ends up in better date format where that function already written should be able to handle it
I know it’s patching but full context is not known to me also so trying to make it work even some invalid data is passed
m
parseDateTime() handles your concern, it converts a string into a date object, and doesn't require the data it receives to be in a strict pattern, then you can use the functions to grab the part you want. i would try catch around the parseDateTime portion, and if you get to the catch, just set to now() or whatever your default is.