Jump to content

Assiging a new value to a field


Recommended Posts

Okay, I have been looking at this too long and it isn't making sense anymore. Below is my code, it breaks down where I try to assign the new value.

 

I am re-formatting a date submitted via a spreadsheet. so their date of "10-Aug-16" or "16 August 2016" or "16-August-2016" returns as "16 August 2016".

 

if (Field("InductionDate") != "")
   {
   var str = Field("InductionDate");
   var arr = str.split(/[- ]+/);

   var day = arr[0];
   var month = arr[1];
   var year = arr[2];

   if (month.length < 4)
   {
   var editmonth = month.replace(/Aug/g,"August")
                    .replace(/Sept/g,"September");
   }
     else
    {
      editmonth = month;
    }

   if (year.length == 4)
   {
   var edityear = year;
   }
     else
    {
      edityear = "20" + year;
    }

   var Field("InductionDate") = day + " " + editmonth + " " + edityear;
   return (Field("InductionDate");
   }
   else
   {
    return "error";
    }

 

My script functions in a normal javascrip environment, but assigning the new value to the FusionPro Field "InductionDate" is where i am failing.

 

I know it is something simple, I just can't see it.

Link to comment
Share on other sites

Figured it out. I was over thinking my code.

 

if (Field("InductionDate") != "")
   {
   var str = Field("InductionDate");
   var arr = str.split(/[- ]+/);

   var day = arr[0];
   var month = arr[1];
   var year = arr[2];

   if (month.length <= 4)
   {
   var editmonth = month.replace(/Jan/g,"January")
                   .replace(/Feb/g,"February")
                   .replace(/Mar/g,"March")
                   .replace(/Apr/g,"April")
                   .replace(/May/g,"May")
                   .replace(/Jun/g,"June")
                   .replace(/Jul/g,"July")
                   .replace(/Julyy/g,"July")
                   .replace(/Aug/g,"August")
                   .replace(/Sept/g,"September")
                   .replace(/Oct/g,"October")
                   .replace(/Nov/g,"November")
                   .replace(/Dec/g,"December");
   }
     else
    {
      editmonth = month;
    }

   if (year.length == 4)
   {
   var edityear = year;
   }
     else
    {
      edityear = "20" + year;
    }

   return day + " " + editmonth + " " + edityear;
   }
   else
   {
    return "error";
    }

Link to comment
Share on other sites

This should work as well:

var str = Field("InductionDate");
var [day, month, year] = str.split(/[-\/ ]+/);
if ([day,year].map(isNaN).some(Boolean))
   return "error";
var Left3Lower = function(s){return(ToLower(Left(s,3)));};
month = Int(month) || MONTH_NAMES.English.map(Left3Lower).indexOf(Left3Lower(month)) + 1;
if (day < 1 || day > 31 || month < 1 || month > 12)
   return "error";
return FormatDate(DateFromString([month,day,year].join("/")), "dd lm yyyy");

Edited by Dan Korn
Added error checking for month and day.
Link to comment
Share on other sites

As would this assuming you're okay with formatting every year as 20##:

var date = Field("InductionDate").replace(/[-\s]|(\d+)$/g, 
 function(s,p) {return p ? '20' + p.slice(-2) : ' ' });
return FormatDate(new Date(date), 'd lm 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...