i am updating an old code to a new function i trie...
# cfml-general
g
i am updating an old code to a new function i tried to updtae this code to
Copy code
<cfif #sAppDateFormat# IS "MM/dd/YYYY">
                <cfset StartDateIn = createdate(mid(FromDate,7,4),mid(FromDate,1,2),mid(FromDate,4,2))>
                <cfset EndDateIn = createdate(mid(ToDate,7,4),mid(ToDate,1,2),mid(ToDate,4,2))>
            <cfelseif #sAppDateFormat# IS "DD/MM/YYYY">
                <cfset StartDateIn = createdate(mid(FromDate,7,4),mid(FromDate,4,2),mid(FromDate,1,2))>
                <cfset EndDateIn = createdate(mid(ToDate,7,4),mid(ToDate,4,2),mid(ToDate,1,2))>
            <cfelse>
                <cfset StartDateIn = createdate(datepart("y",FromDate),datepart("m",FromDate),datepart("d",FromDate))>
                <cfset EndDateIn = createdate(datepart("y",ToDate),datepart("m",ToDate),datepart("d",ToDate))>
            </cfif>
to
Copy code
<cffunction name="udfParseDate" returntype="date" output="false">
        <cfargument name="sAppDateFormat" type="string" required="true">
        <cfargument name="dateString" type="string" required="true">
        <cfset var parsedDate = "">
        <cfif sAppDateFormat EQ "MM/dd/YYYY">
            <cfset parsedDate = ParseDateTime(dateString, "MM/dd/YYYY")>
        <cfelseif sAppDateFormat EQ "DD/MM/YYYY">
            <cfset parsedDate = ParseDateTime(dateString, "DD/MM/YYYY")>
        <cfelse>
            <cfthrow message="Unsupported date format. Please use MM/DD/YYYY or DD/MM/YYYY.">
        </cfif>
        <cfreturn parsedDate>
    </cffunction>
trying to ask for suggestion, it seems right to me, but still asking
r
ParseDateTime doesn't work like that. Lookup LSParseDateTime if you want to parse different date formats (locales).
g
i want to return the date as : 2023-10-01 if mm/dd/yyyy is passed and 2023-01-10 if dd/mm/yyyy is passed
r
Then use DateFormat if you want a string, but the original code is creating a date based on a string, which is different then what you just stated.
g
I dumped the code which is using a mid function and that is what it outputs to me A date starting with { ts
r
Right, it's returning a date object, not a string. They are not the same, although CF will try to convert between the two.
💯 1
g
ok, so what can i actually do yhere
will this work
Copy code
<cffunction name="formatDate" returntype="string">
        <cfargument name="dateString" type="string" required="true">
        <cfargument name="dateFormat" type="string" required="true">
    
        <cfset var formattedDate = "">
    
        <cfif dateFormat EQ "MM/dd/YYYY">
            <cfset formattedDate = DateFormat(dateString, "yyyy-mm-dd")>
        <cfelseif dateFormat EQ "DD/MM/YYYY">
            <cfset formattedDate = DateFormat(dateString, "yyyy-dd-mm")>
        <cfelse>
            <cfset formattedDate = createdate(datepart("y", dateString), datepart("m", dateString), datepart("d", dateString))>
            <cfset formattedDate = DateFormat(formattedDate, "yyyy-mm-dd")>
        </cfif>
    
        <cfreturn formattedDate>
    </cffunction>
r
I don't know, but probably not. I'll repeat myself again. The original code returns a DATE, your function returns a string which can be ambiguous. Either use the original code that returns a date or use LSParseDateTime if you know the format (locale) of the the incoming date string.
👆 1