Search code examples
coldfusioncoldfusion-8

How can I extract a date substring from a date+time string in Coldfusion?


I'm a newbie in Coldfusion. I'm trying to change a string that looks like "Jun 11, 2014, 8:50 PM" to "Jun 11, 2014". I tried to use

<cfset album[currentrow]['date'] = ListGetAt(album[currentrow]['date'], 2, ",")>

It gives me 2014. If I change 2 to 1, I get Jun 11. Can someone please give me some advice on if there's a method to extra everything before the second ","? Your help is greatly appreciated.


Solution

  • If you can guarantee that the string is always formed of three comma-separated parts, and the date is always the first two, then you can do this:

    dateTimeString = "Jun 2, 2014, 6:20 PM";
    dateString = listDeleteAt(dateTimeString, 3);
    date = parseDateTime(dateString);
    dateFormattedForOutput = dateFormat(date, "mmm d, yyyy");
    

    Note that you should only ever use dateFormattedForOutput for output; if you're storing the date or manipulating it, use date.