Jump to content

Format Date in Spanish


apalmer

Recommended Posts

I've been formatting the dates in my pieces successfully using:

 

var MyDate = Field("Date1");

 

return FormatDate(MyDate, "ld, lm d, yyyy");

 

Is there a way to have the ld, lm, d, yyyy show up in a Spanish format? I see that the correct way to format a date in Spanish is (ld d de lm de yyyy) like so: martes 12 de octubre de 2011.

 

Angie

Edited by apalmer
it was cut off.
Link to comment
Share on other sites

Try this:

function SpanishDate(myDate)
{
   var months = ['enero','febrero','marzo','abril','mayo','junio','julio','agosto','septiembre','octubre','noviembre','diciembre'];
   var days = ['domingo','lunes','martes','miércoles','jueves','viernes','sábado'];

   var month = months[myDate.getMonth()];
   var day = days[myDate.getDay()];
   return day + " " + myDate.getDate() + " de " + month + " de " + myDate.getFullYear();
}

return SpanishDate(Today());

Link to comment
Share on other sites

That worked great. But I need to tweak it so that it's whatever date the user enters into the field, not just today's date. I have a couple of fields that will be filled out and want all of them to have the Spanish format.

 

Field("Date1")

Field("Date2")

Field("Date3")

Field("Date4")

 

Any ideas?

Link to comment
Share on other sites

That worked great. But I need to tweak it so that it's whatever date the user enters into the field, not just today's date. I have a couple of fields that will be filled out and want all of them to have the Spanish format.

 

Field("Date1")

Field("Date2")

Field("Date3")

Field("Date4")

 

Any ideas?

Sure, just call the function with whatever date you want, like so:

return SpanishDate(DateFromString(Field("Date1")));

You can put the SpanishDate function in your JavaScript Globals if you need to access it from multiple rules.

Link to comment
Share on other sites

  • 4 years later...

This will return the Spanish date if the language is set to "Spanish," for anything else it will return English:

function SpanishDate(myDate)
{
   var months = ['enero','febrero','marzo','abril','mayo','junio','julio','agosto','septiembre','octubre','noviembre','diciembre'];
   var days = ['domingo','lunes','martes','miércoles','jueves','viernes','sábado'];

   var month = months[myDate.getMonth()];
   var day = days[myDate.getDay()];
   return day + " " + myDate.getDate() + " de " + month + " de " + myDate.getFullYear();
}

return (FusionPro.language == "Spanish") ? SpanishDate(Today()) : FormatDate(Today(), 'lm d, yyyy');

Link to comment
Share on other sites

This will return the Spanish date if the language is set to "Spanish," for anything else it will return English:

Yes, it will, although I think the question was about the value from a data field named "language", rather than about the language under which FusionPro's UI is running:

Is there a way that I can make this rule dependent upon the "language" field?

So the last line would be this instead:

return (Field("language") == "Spanish") ? SpanishDate(Today()) : FormatDate(Today(), 'lm d, yyyy');

However, in FusionPro 9.2 or later, the day and month names are already translated into Spanish, as well as German, French, Italian, Japanese, and Chinese (both traditional and simplified), so you don't need to provide your own translations. You just need to specify the language as the (optional) third parameter to FormatDate, like so:

return FormatDate(Today(), "EE, lm d, yyyy", "Spanish");

Just that one liner is all you need.

 

Or specify any of those other languages. This should work for mdlivels:

return FormatDate(Today(), "EE, lm d, yyyy", Field("language"));

Or, if you want to do that based on FusionPro's installed language:

return FormatDate(Today(), "EE, lm d, yyyy", FusionPro.language);

Link to comment
Share on other sites

  • 4 years later...
Is there a way to use this same formula but also pull in a date field from the data instead of using Today's Date?

 

Sure, something like:

return FormatDate(DateFromString(Field("Birthday")), "EE, lm d, yyyy", "Spanish");

Be aware that the DateFromString function makes certain assumptions about the format of the string it's parsing, such as favoring US-style mm/dd/yyyy over European-style dd/mm/yyyy, for one.

Link to comment
Share on other sites

Thank you! I ended up with this as my final rule. It displays the date in English or Spanish based on a language column in the data as well as a predetermined date that is also in the data:

 

function SpanishDate(myDate)

{

var months = ['enero','febrero','marzo','abril','mayo','junio','julio','agosto','septiembre','octubre','noviembre','diciembre'];

var letterdate = Field("letter date")

var month = months[myDate.getMonth()];

return myDate.getDate() + " de " + month + " " + myDate.getFullYear();

}

 

return (Field("language") == "S") ? SpanishDate(DateFromString(Field("letter date"))) : FormatDate(DateFromString(Field("letter date")), 'lm d, yyyy');

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...