ColdFusion: how to parse dd / mm / yyyy date?

I found the ParseDateTime function , but it only parses a date / time string according to language conventions in English (USA).

How to analyze the date, which is in the format dd / mm / yyyy?

Try the following:

<cfset TestdateFrom = ParseDateTime("10/9/2010") />
<cfloop index="i" from="1" to="30" step="1">
    <cfset TestdateFrom = DateAdd( "d", 1, TestdateFrom ) />
    #TestdateFrom#<br/>
</cfloop>

CF9 has an LSParseDateTime function .

I don’t know if this will help me or not.

Finally, should I use the java library for this problem?

+3
source share
4 answers

I had success in this technique for working with dates (formatted in the UK) entered as free text:

<cfset SetLocale("English (UK)")>
<cfset valid = true>

<!--- Convert DD-MM-YYYY or DD.MM.YYYY to DD/MM/YYYY --->
<cfset dt = replacelist(dt, ".,-", "/,/")>

<!--- count date elememnts (must be 3 - LSParseDateTime will make potentially incorrect assumptions otherwise) --->
<cfif listlen(dt, "/") neq 3>
    <!--- wrong number of elements --->
    <cfset valid = false>
<cfelse>
    <!--- Correct number of elements so try to interpret as date/time object --->   
    <cftry>
        <cfset dt = LSParseDateTime(dt)> 
        <cfcatch type="Expression">
            <cfset valid = false>
        </cfcatch>
    </cftry>
</cfif>

true, dt /. DD.MM.YYYY DD-MM-YYYY, DD/MM/YYYY.

+3

, :

<cfset formatter = createObject("java","java.text.SimpleDateFormat")>
<cfset formatter.init("dd/MM/yyyy")>
<cfset newDate = formatter.parse("10/09/2010")>
#newDate#

?

+6

, - :

<cfset dy=listGetAt(dateString,1,"/")>
<cfset mo=listGetAt(dateString,2,"/")>
<cfset yr=listGetAt(dateString,3,"/")>

<cfset myDate=createDate(yr,mo,dy)>

, , .

+5

lsParseDateTime(). .

, setlocale(), .

, LS.

+2

Source: https://habr.com/ru/post/1775369/


All Articles