Home › Forums › Classic DFFS › Set field value to yyyymm of date field in form
- This topic has 5 replies, 3 voices, and was last updated 3 years ago by Alexander Bautz.
-
AuthorPosts
-
-
June 4, 2021 at 16:32 #33720
Hi! I want to set a rule that sets a field value based off a date field. I want the field value to be yyyymm (202105) based off the date a user enters.
I see you can do somwthing like {Timestamp[yyyy]} but that doesnt work for an internal field name
- This topic was modified 3 years, 6 months ago by Paul Heffner.
-
June 7, 2021 at 11:49 #33730
Hi,
There is unfortunately no such formatting option built in, but you can do it with some custom js.Add this to your Custom JS:
function setCustomDateStr(){ var d = spjs.utility.getDateFieldAsDateObject("YOUR_DATE_FIELD"); var dStr = d.getFullYear() + String(d.getMonth() + 1).padStart(2,"0"); setFieldValue("YOUR_TEXT_FIELD", dStr); }
Now set up a rule to trigger on change on your date field, and in the field “Run these functions / evaluate these rules” add the function name setCustomDateStr.
Change YOUR_DATE_FIELD and YOUR_TEXT_FIELD to match your fields.
Alexander
-
June 9, 2021 at 18:33 #33754
I am getting the error the objext doesnt support padStart
-
June 9, 2021 at 18:45 #33756
I forgot padStart isn’t available in IE – try it like this instead:
function setCustomDateStr(){ var d = spjs.utility.getDateFieldAsDateObject("YOUR_DATE_FIELD"); var month = d.getMonth() + 1; if(month < 10){ month = "0" + month; } var dStr = d.getFullYear() + month; setFieldValue("YOUR_TEXT_FIELD", dStr); }
Alexander
-
-
December 15, 2021 at 14:25 #35405
I’m trying to use this but, it is giving me an error. Here’s the code I’m using:
function setCustomDateStr(){
var d = spjs.utility.getDateFieldAsDateObject(“DateServicesProvided”);
var dStr = d.getFullYear() + String(d.getMonth() + 1).padStart(2,”0″);
setFieldValue(“YearMonth”, dStr);
}When the date changes, I get an error that says the “TypeError: d.getFullYear is not a function”. If I click OK, the function works and the correct number is filled in. But how do I get rid of that error?
-
December 15, 2021 at 17:01 #35407
Try adding an alert (or console.log) to see what the value for d is.
It might be that you need a little timeout to let the picker “finish” – try it like this:
function setCustomDateStr(){ setTimeout(function(){ var d = spjs.utility.getDateFieldAsDateObject("DateServicesProvided"); var dStr = d.getFullYear() + String(d.getMonth() + 1).padStart(2,"0"); setFieldValue("YearMonth", dStr); },100); }
Alexander
-
-
AuthorPosts
- You must be logged in to reply to this topic.