Not yet rated

Problem

You need to convert a ColdFusion date into a JavaScript date object to use in your JavaScript code.

Solution

Create a small UDF for generating the necessary code to create a JavaScript date object from a ColdFusion date object.

Detailed explanation

The following UDF allows you to generate the necessary JavaScript code to use any ColdFusion date within the JavaScript on your page:

<cfscript>
function jsDateFormat(date){
    if( isDate(date))    return 'new Date(#year(date)#,
#(month(date)-1)#, #day(date)#, #hour(date)#, #minute(date)#,
#second(date)#)';
    else return "null";
}
</cfscript>

Now any time you need to use the date object within your JavaScript code, just do:

<script type="text/javascript">
var today = <cfoutput>#jsDateFormat(now())#</cfoutput>;
alert(today);
</script>

 


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes