Go Back   MarcomCentral (PTI) and FusionPro User Communities > Software-Related Talk > The JavaScript Library > Formatting Text

Notices

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old September 3rd, 2009, 10:07 AM
Ryan Graybeal's Avatar
Ryan Graybeal Ryan Graybeal is offline
Junior Community Member
 
Join Date: Sep 2008
Location: Seattle Washington
Posts: 38
Default format date function problem

I am having a problem with the FormatDate function.

My data is "8/25/09"


Code:
return (FormatDate(Field("date***************]"), "ld, lm d, yyyy"))
The rule returns "Wednesday 25, 1909"

Most of us have moved past the 1900's.

What can I do to get the rule to return "Wednesday 25, 2009"
???????????
__________________
Ryan Graybeal
ColorGraphics

A Cenveo Company

FP 9.3.6 Mac OS X 10.7.5
Acrobat 10
ryan.graybeal@cenveo.com
Reply With Quote
  #2  
Old September 3rd, 2009, 01:36 PM
tobarstep tobarstep is offline
Regular Contributor
 
Join Date: Sep 2008
Location: Florida
Posts: 71
Default Re: format date function problem

FormatDate seems to want to use 1900 as default. Fortunately DateFromString seems to be firmly grounded in the 21st century. This worked for me
Code:
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.
__________________
FusionPro VDP 9.3.12 Desktop/Server
Windows 7 Enterprise / Windows Server 2008 r2
Acrobat X
Reply With Quote
  #3  
Old September 3rd, 2009, 01:50 PM
rpaterick rpaterick is offline
Senior Contributor
 
Join Date: Mar 2009
Posts: 427
Default Re: format date function problem

8/25/2009

Code:
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.
__________________
Windows 7 64BIT
Intel - I7 chip - 3.33Ghz - 6GB ram
8.0 Creator/8.0 Producer/CS5.5

Last edited by rpaterick; September 3rd, 2009 at 02:03 PM..
Reply With Quote
  #4  
Old September 3rd, 2009, 02:03 PM
Ryan Graybeal's Avatar
Ryan Graybeal Ryan Graybeal is offline
Junior Community Member
 
Join Date: Sep 2008
Location: Seattle Washington
Posts: 38
Default Re: format date function problem

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.

Code:
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+1900);} // I change this to 2000
    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.

Code:
return (FormatDate(Field("date***************]"), "lm d, yyyy"))
Thanks again.

Ryan
__________________
Ryan Graybeal
ColorGraphics

A Cenveo Company

FP 9.3.6 Mac OS X 10.7.5
Acrobat 10
ryan.graybeal@cenveo.com
Reply With Quote
  #5  
Old September 3rd, 2009, 02:05 PM
rpaterick rpaterick is offline
Senior Contributor
 
Join Date: Mar 2009
Posts: 427
Default Re: format date function problem

Quote:
Originally Posted by Ryan Graybeal View Post
Thanks for all of you help.

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

Thanks again.

Ryan
I modified my post a little more clearer.
__________________
Windows 7 64BIT
Intel - I7 chip - 3.33Ghz - 6GB ram
8.0 Creator/8.0 Producer/CS5.5
Reply With Quote
  #6  
Old March 29th, 2010, 02:47 PM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 955
Default Re: format date function problem

Code:
return FormatDate(Field("date"), "ld, lm d, 20yy");
Reply With Quote
  #7  
Old March 29th, 2010, 03:31 PM
Dan Korn's Avatar
Dan Korn Dan Korn is offline
FusionPro Senior Engineer / Forum Moderator
 
Join Date: Aug 2008
Location: Chicago, IL
Posts: 4,366
Default Re: format date function problem

Quote:
Originally Posted by step View Post
Code:
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.
__________________
Dan Korn
FusionPro Developer / JavaScript Guru / Forum Moderator
PTI Marketing Technologies | Printable | MarcomCentral
LinkedIn

I am a not a Support engineer, and this forum is not a substitute for Support. My participation on this forum is primarily as a fellow user (and a forum moderator). I am happy to provide help and answers to questions when I can; however, there is no guarantee that I, or anyone else on this forum, will be able to answer all questions or fix any problems. If I ask for files to clarify an issue, I might not be able to look at them personally. I am not able to answer private messages, emails, or phone calls unless they go through proper Support channels. Please direct any sales or pricing questions to your salesperson or inquiries@marcom.com.

Complex template-building questions, as well as all installation and font questions or problems, should be directed to FusionProSupport@marcom.com. Paid consulting work may be required to fulfill your template-building needs.

This is a publicly viewable forum. Please DO NOT post fonts, or other proprietary content, to this forum. Also, please DO NOT post any "live" data with real names, addresses, or any other personal, private, or confidential data.

Please include the specific versions of FusionPro, Acrobat, and your operating system in any problem reports or help requests. I recommend putting this information in your forum signature. Please also check your composition log (.msg) file for relevant error or warning messages.

Please post questions specific to the MarcomCentral Enterprise and Web-to-Print applications in the MarcomCentral forum. Click here to request access. Or contact your Business Relationship Manager (BRM/CPM) for assistance.

Please direct any questions specific to EFI's Digital StoreFront (DSF) to EFI support.

How To Ask Questions The Smart Way

The correct spellings are JavaScript, FusionPro, and MarcomCentral (each with two capital letters and no spaces). Acceptable abbreviations are JS, FP, and MC (or MCC). There is no "S" at the end of "Expression" or "Printable"! The name of the product is FusionPro, not "Fusion". "Java" is not is not the same as JavaScript.

Check out the JavaScript Guide and JavaScript Reference! FusionPro 8.0 and newer use JavaScript 1.7. Older versions use JavaScript 1.5.

return "KbwbTdsjqu!spdlt\"".replace(/./g,function(w){return String.fromCharCode(w.charCodeAt()-1)});
Reply With Quote
Reply

Tags
date, format date

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 07:44 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) 2011, PTI Marketing Technologies™, Inc.