Jump to content

format date function problem


Ryan Graybeal

Recommended Posts

I am having a problem with the FormatDate function.

 

My data is "8/25/09"

 

 

return (FormatDate(Field("date***************]"), "ld, lm d, yyyy"))

The rule returns "Wednesday 25, 1909" :confused:

 

Most of us have moved past the 1900's.

 

What can I do to get the rule to return "Wednesday 25, 2009"

???????????

Link to comment
Share on other sites

FormatDate seems to want to use 1900 as default. Fortunately DateFromString seems to be firmly grounded in the 21st century. This worked for me

return FormatDate(DateFromString("8/25/09"), "ld, lm d, yyyy");

 

Obviously replace the date literal with your field.

 

I would suggest looking into getting the data formatted with 4 digit years if possible. From a programming standpoint it's the way to go.

Link to comment
Share on other sites

8/25/2009

 

return FormatDate(Field("date"), "ld, lm d, yyyy");

would look like: Tuesday, August 25, 2009

 

This will pull your date from the data and format it for different dates that you may have throughout the run. You could modify it to just read Tuesday, 25, 2009 if you wanted to.

Link to comment
Share on other sites

Thanks for all of you help.

 

I did find a solution. Here is what I did.

 

I went in and found the Builtins.js file ---

(mac) library/application support/Printable/FusionPro

 

I opened it in a text editor and found the function for FormatDate

 

I then copied the function into my FP rule.

 

function FormatDate(date_, format, lang)
{
   var _lang = ToTitleCase(Trim("" + (lang || "English"))); // FusionPro.language
   if (typeof MONTH_NAMES[_lang] == "undefined")
   {
     var names = "";
     for (var i in MONTH_NAMES)
       names += (names ? ", " : "") + '"' + i + '"';
     ThrowError("FormatDate", TranslateAndBuildMessage("Invalid language: \"{0}\".  Must be one of: {1}.", lang, names));
   }

try {    
   format = ReplaceSubstring(format,"sd","E")
   format = ReplaceSubstring(format,"ld","EE")
   format = ReplaceSubstring(format,"sm","NNN")
   format = ReplaceSubstring(format,"lm","MMM")
   //format = ReplaceSubstring(format,"mm","MM")
   format = ReplaceSubstring(format,"m","M")

   format=format+"";
   var result="";
   var i_format=0;
   var c="";
   var token="";
 var date = new Date(date_);
   var y=date.getYear()+"";
   var M=date.getMonth()+1;
   var d=date.getDate();
   var E=date.getDay();
   var H=date.getHours();
   var m=date.getMinutes();
   var s=date.getSeconds();
   var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k;
   // Convert real date parts into formatted versions
   var value=new Object();
   if (y.length < 4) {y=""+(y-0+[color=Red][i][b]1900[/b][/i][/color]);}[color=Red][i][b] // I change this to 2000[/b][/i][/color]
   value["y"]=""+y;
   value["yyyy"]=y;
   value["yy"]=y.substring(2,4);
   value["M"]=M;
   value["MM"]=LZ(M);
   value["MMM"]=MONTH_NAMES[_lang][M-1];
   value["NNN"]=Left(MONTH_NAMES[_lang][M-1], 3);
   value["d"]=d;
   value["dd"]=LZ(d);
   value["E"]=Left(DAY_NAMES[_lang][E], 3);
   value["EE"]=DAY_NAMES[_lang][E];
   value["H"]=H;
   value["HH"]=LZ(H);
   if (H==0){value["h"]=12;}
   else if (H>12){value["h"]=H-12;}
   else {value["h"]=H;}
   value["hh"]=LZ(value["h"]);
   if (H>11){value["K"]=H-12;} else {value["K"]=H;}
   value["k"]=H+1;
   value["KK"]=LZ(value["K"]);
   value["kk"]=LZ(value["k"]);
   value["a"]= (H > 11) ? "pm" : "am";
 value["A"] = ToUpper(value["a"]);
   value["m"]=m;
   value["mm"]=LZ(m);
   value["n"]=m;
   value["nn"]=LZ(m);
   value["s"]=s;
   value["ss"]=LZ(s);
   while (i_format < format.length) {
       c=format.charAt(i_format);
       token="";
       while ((format.charAt(i_format)==c) && (i_format < format.length)) {
           token += format.charAt(i_format++);
           }
       if (value[token] != null)         
       { result=result + value[token]; }
       else { result=result + token; }
       }
   return result;
}
catch (e)
{
   ThrowError("FormatDate", "The input string is invalid.");
   //return inputString;
}
}// end function formatDate(date,format)

//__________________________________________________________________
FormatDate.builtin = true;

 

I had to ad one piece of code at the bottom of the rule to get the function to work.

 

return (FormatDate(Field("date***************]"), "lm d, yyyy"))

 

Thanks again.

 

Ryan

Link to comment
Share on other sites

  • 6 months later...
return FormatDate(Field("date"), "ld, lm d, 20yy");

Please don't do that. That's still the same Y2K problem, just making a different assumption. What if the field represents a birthday? I won't tell you exactly how old I am, but I definitely wasn't born in this century.

 

If the input data is not specifying a full four-digit year, then the ambiguity should be corrected on that end, not in a composition-time rule.

 

That said, if legacy input data with a two-digit year format is all you've got, then you'll have to make some kind of assumption. As tobarstep noted (way back in Aught Nine), the DateFromString function makes a different one; specifically, it treats any number between than 0 and 29 as this century (2000 through 2029), and any number between 30 and 99 as in the previous century (1930 through 1999). But it's still an assumption, one which is not guaranteed to be correct, either now or in the future.

 

Another common ambiguity in input date formats is day versus month numbers. For instance, "10/3" is generally understood as October 3rd in the U.S., but as March 10th in the U.K. But that's a different discussion.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...