views:

836

answers:

2

Is anyone aware of a simple way to convert mysql datetime values to coldfusion datetime values in CF8 (and it may have to be backwards compatible with CF6)?

I need to store date times in mysql and have chosen to store them in mysql datetime format so I can get the db to do date ranges and comparisons for me. I could so this stuff in CF, but I'd have to retrieve potentially huge result set to process. Seems much saner to get the db to deal with this for me.

My front end code is coldfusion, and converting the cf date time objects into something mysql understands on insert is easy enough. But I've run into trouble bringing them back the other way.

There is the CreateDateTime function, but I would first have to split the mysql result up into the separate year, month, day, etc parts first. That's not hard, but seems needlessly complicated compared to converting the dates in the other direction.

The ParseDateTime function is no good to me either because I want to do further processing on a datetime object server side before formatting and sending to the client.

Does anyone know of a simple way to do this? Or do I have to settle for an easy conversion one way, and a kludgy one the other way?

+2  A: 

you can use CreateODBCDateTime(date) function for this.

Giriraj Govil
Uh...wrong way. This formats a ColdFusion date for insertion *into* MySQL. The OP is looking at converting from MySQL format to the ColdFusion format.
Eric
+3  A: 

date_format is very good for this:

date_format(myDateCol, '%m/%d/%Y')

Moreover, if you need to date calculations on this, date_format can just be your wrapper:

date_format(date_add(myDateCol, interval 7 day), '%m/%d/%Y')

That will format the date for a week from now (+7 days) into the ColdFusion format.

Eric
Thanks for that. It's exactly what I needed. I didn't know I could get mysql to do the formatting for me (so much to learn). I am now a happier person.
nedlud
Glad I could help! MySQL and SQL in general has a lot of really powerful stuff that often gets overlooked. It's worth spending a day or two delving through the docs to find out what you don't know!
Eric
Okay, not to belabour the point ;).. but I thought the OP wanted a datetime object? The documentation says date_format function returns a string.
Leigh
@Leigh: The OP has commented saying that he got "exactly what [he] needed" and has marked this as the answer. Why do you say that this isn't what the OP needed? `date_format` returns a string as far as MySQL goes, but to ColdFusion, data is data, and it interprets it how it will. The MySQL data type of the return column has little to no bearing on how CF handles it.
Eric
@Eric - They asked how to "convert mysql _datetime_ values to coldfusion _datetime_ values". A datetime object has a specific meaning. It is not the same as a string, even in CF. While CF is loose about types on the surface, there is a difference between strings, datetimes, numeric values, etcetera. They can be represented by totally different classes behind the scenes which affects the behavior when you use them. So the MySQL data type of the column has a LOT to do with how CF handles it. The data type is used to determine what underlying java class is used to represent the column values.
Leigh
Example: <cfquery name="q" datasource="#dsn#"> SELECT date_format(someDateColumn, '%m/%d/%Y') AS dateString, someDateColumn AS dateTimeObject FROM SomeTable</cfquery><cfoutput>dateString = #q.dateString[1].getClass().getName()#<br>dateTimeObject = #q.dateTimeObject[1].getClass().getName()#<br></cfoutput>
Leigh