Format your date in spanish.

Format your date in spanish With ColdFusion!

The following custom tag will allow you to display your dates in spanish, let me show you how the tag itself works:

<!--- All you have to do is call the function as
   <cf_dateformates date = (a ODBC date) format = "(the format you want it to print out)"
         formats:
         yyyy eq. 2004
         yy eq. 04
         y eq. 4
         mmmm eq. Enero
         mmm eq. Ene
         mm eq. 01
         m eq. 1
         dddd eq. Lunes
         ddd eq. Lun
         dd eq. 01
         d eq. 1
         Date:
             Make sure that it is a ODBC date, to make it a odbcdate you can apply the following.
             createODBCdate(createdate(year,month,day))
             or for the server current date you can do
             createODBCdate(now())
             good luck and enjoy translating your date to spanish.
--->

<cfif Not IsDefined("Attributes.Date") and Not IsDefined("Attributes.Format")>

<font color="#FF0000" size="4" face="Verdana, Arial, Helvetica, sans-serif">
    You have to specify a date.</font>
    <cfabort>
</cfif>

<!--- first define the months in spanish --->
<cfif dateformat(Attributes.Date, 'mmmm') EQ 'January'>
    <cfset month = "Enero">
<cfelseif dateformat(Attributes.Date, 'mmmm') EQ 'February'>
    <cfset month = "Febrero">
<cfelseif dateformat(Attributes.Date, 'mmmm') EQ 'March'>
    <cfset month = "Marzo">
<cfelseif dateformat(Attributes.Date, 'mmmm') EQ 'April'>
    <cfset month = "Abril">
<cfelseif dateformat(Attributes.Date, 'mmmm') EQ 'May'>
    <cfset month = "Mayo">
<cfelseif dateformat(Attributes.Date, 'mmmm') EQ 'June'>
    <cfset month = "Junio">
<cfelseif dateformat(Attributes.Date, 'mmmm') EQ 'July'>
    <cfset month = "Julio">
<cfelseif dateformat(Attributes.Date, 'mmmm'') EQ 'August'>
    <cfset month = "Agosto">
<cfelseif dateformat(Attributes.Date,'mmmm') EQ 'September'>
    <cfset month = "Septiembre">
<cfelseif dateformat(Attributes.Date, 'mmmm') EQ 'October'>
    <cfset month = "Octubre">
<cfelseif dateformat(Attributes.Date,'mmmm') EQ 'November'>
    <cfset month = "Noviembre">
<cfelseif dateformat(Attributes.Date, 'mmmm') EQ 'December'>
    <cfset month = "Diciembre">
</cfif>

<!--- Now define the days in spanish --->
<cfif dateformat(Attributes.Date, 'dddd') EQ 'Monday'>
    <cfset day = "Lunes">
<cfelseif dateformat(Attributes.Date, 'dddd') EQ 'Tuesday'>
    <cfset day = "Martes">
<cfelseif dateformat(Attributes.Date, 'dddd') EQ 'Wednesday'>
    
<cfset day = "Miércoles">
<cfelseif dateformat(Attributes.Date, 'dddd') EQ 'Thursday'>
    
<cfset day = "Jueves">
<cfelseif dateformat(Attributes.Date, 'dddd') EQ 'Friday'>
    
<cfset day = "Viernes">
<cfelseif dateformat(Attributes.Date, 'dddd') EQ 'Saturday'>
    
<cfset day = "Sabado">
<cfelseif dateformat(Attributes.Date, 'dddd') EQ 'Sunday'>
    
<cfset day = "Domingo">
</cfif>

<!--- now process the actual tag to return values, First do the months values --->
<cfif find("mmmm","#attributes.format#")>
    <cfset attributes.format = replace(attributes.format,'mmmm','#month#')>
<cfelseif find("mmm","#attributes.format#")>
    
<cfset attributes.format = replace(attributes.format,'mmm','#left("#month#", 3)#')>
<cfelseif find("mm","#attributes.format#")>
    
<cfset attributes.format = replace(attributes.format,'mm','#dateformat(Attributes.Date, 'mm')#')>
<cfelseif find("m","#attributes.format#")>
    
<cfset attributes.format = replace(attributes.format,'m','#dateformat(Attributes.Date, 'm')#')>
</cfif>

<!--- Next, do the days values --->
<cfif find("dddd","#attributes.format#")>
    
<cfset attributes.format = replace(attributes.format,'dddd','#day#')>
<cfelseif find("ddd","#attributes.format#")>
    
<cfset attributes.format = replace(attributes.format,'ddd','#left("#month#",3)#')>
<cfelseif find("dd","#attributes.format#")>
    
<cfset attributes.format = replace(attributes.format,'dd','#dateformat(Attributes.Date, 'dd')#')>
<cfelseif find("d","#attributes.format#")>
    
<cfset attributes.format = replace(attributes.format,'d','#dateformat(Attributes.Date, 'd')#')>
</cfif>


<cfif find("yyyy","#attributes.format#")>
    
<cfset attributes.format = replace(attributes.format,'yyyy','#dateformat(Attributes.Date, 'yyyy')#')>
</cfif>

<!--- Next, do the years values --->
<cfif find("yy","#attributes.format#")>
    
<cfset attributes.format = replace(attributes.format,'yy','#dateformat(Attributes.Date, 'yy')#')>
</cfif>
<cfif find("y","#attributes.format#")>
    <cfset attributes.format = replace(attributes.format,'y','#dateformat(Attributes.Date, 'y')#')>
</cfif>

<!--- now, let's pass the value on the page, for the visitor to see --->
<cfoutput>#attributes.format#</cfoutput>

That's it, you can now have your dates displayed in spanish!



All ColdFusion Tutorials By Author: Diego Benitez
  • Format your date in spanish.
    This is a custom tag to translate the regular english output of a date into spanish like (20 de Enero de 2004) and as you want it to be.
    Author: Diego Benitez
    Views: 9,126
    Posted Date: Thursday, January 29, 2004
  • how to format a date/time using bind
    here is a long way but the only one i found to format a date/time using the <cfformgroup type="repeater" query="queryname">
    Author: Diego Benitez
    Views: 7,389
    Posted Date: Thursday, March 10, 2005
  • QueryStringSet
    Replaces or Adds a variable with its value on a cgi.Query_String alike. It supports multiple values at the same time separated by commas
    Author: Diego Benitez
    Views: 5,502
    Posted Date: Wednesday, January 11, 2006