Reputation: 619
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.
Upvotes: 0
Views: 1512
Reputation: 29870
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
.
Upvotes: 2
Reputation: 742
Messing with strings isn't tough, but since you are working with a date, lets use the Date Functions. If you have a string, the best thing to do is convert it into a Date Object, using
<cfset myDate = ParseDateTime(string)>
Then once it is a Date Object, you can do whatever you want with it. Use Dateformat to manipulate it as needed.
<cfoutput>#dateformat(myDate, "mmm dd, yyyy")#</cfoutput>
ParseDateTime documentation here
Edit - Using Strings instead.
You can use LEFT to get the left piece of the string. To know how many characters your want, you need to find the location of the second "," comma. Assuming the format is consistent, the first comma should be no more than 8 characters in, so we use FIND to look for "," in the string, starting at position 8.
<cfset theLoc = find(",", album[currentrow]['date'], 8) >
Then we use the left function to get the characters, but we do not want the comma, so we take 1 off of it.
<cfset theDate = left( album[currentrow]['date'], theLoc- 1 )>
<cfoutput>#theDate#</cfoutput>
You can do it all inline, but its a little messier
<cfset theDate = left( album[currentrow]['date'], find(",", album[currentrow]['date'], 8)- 1 )>
Upvotes: 3