Search code examples
stringtimecoldfusioncoldfusion-9coldfusion-10

Remove time zones from a string or format HTTP TIME STRING in coldfusion


I am formatting a normal string containing 11:00 PM or 23:00:00 using timeFormat() like this:

<cfset local.timeString = "11:00 PM">
<cfdump var="#local.timeString#">
<cfset local.newTimeString = timeFormat(local.timeString, "HH:mm:ss")>
<cfdump var="#local.newTimeString#">

Output:

11:00 PM 23:00:00

But in one case I came across a time string which also contains a time zone like this: 11:00 PM EDT, so when I was trying to format this string using timeFormat then I was getting error which is correct.

11:00 PM EDT is an invalid date or time string

This type of string can be generated using GetHttpTimeString.

Do I need to use something like this? It is not a correct way. Just thought this as found no other solution.

<cfset local.newTimeString = left(local.timeString
                                , len(local.timeString) - len(listLast(local.timeString," "))
                                )>

Is there any other function to format a string like this.

Please help. Thanks in advance.


Solution

  • If the built in formatting functions arent working, you could dip down to java and use the SimpleDateFormat class.

    // "hh" - 12 hour clock, "HH" - 24 hour clock
    sdf = CreateObject("java","java.text.SimpleDateFormat").init("hh:mm aa zzz");
    testdate = "11:00 PM EDT";
    writeDump(sdf.parse(testdate));  // returns {ts '1970-01-01 22:00:00'}