Copy link to clipboard
Copied
placed in dateObject BirthDate custom calc script
var BirthDay = this.getField("CustomerDOB").value
var BirthDayMonth = util.scand("mm/dd/yyyy", BirthDay.slice(0,6));
event.value = util.printd("mm/dd/yyyy", BirthDayMonth);}
When I enter the actual birthdate in CustomerDOB dateObject, it keeps the day and month values but brings the year current, and displays this date value in the BirthDate dateObject. Problem is when I clear form or use the reset button the current date is populated into BirthDate dateObject and this current date remains. I've looked at similar posts and their solutions but nothing seems to work. How do I get rid of it???????? I attached the file so you can see the setup- thanks in advance
Yes, because code is not meant for that format
By @Asim123
Yes, and that's exactly the problem with it. You have to take into account things the end-user can do, even if they're not what you intend them to do... As long as they can do it, I can assure you they will do it.
Well if you want to be tehnical use your code and enter 02/29/1980 for example
By @Asim123
That is an extremely narrow issue, and a very unlikely one, but you're right that technically it will produce an incorrect res
...Copy link to clipboard
Copied
using .valueAsString also has the same result
Copy link to clipboard
Copied
ok I realize by not specifying date in CustomerDOB it autopopulates using the system's current date/time settings. Is there a way I can do this? I tried unsuccessively
if (CustomerDOB === "") {event.value = "";}
else if {
var BirthDay = this.getField("CustomerDOB").valueAsString;
var BirthDayMonth = util.scand("mm/dd/yyyy", BirthDay.slice(0,6));
event.value = util.printd("mm/dd/yyyy", BirthDayMonth);
}
Copy link to clipboard
Copied
"if (CustomerDOB === "") {event.value = "";}
else if {
var BirthDay = this.getField("CustomerDOB").valueAsString;
var BirthDayMonth = util.scand("mm/dd/yyyy", BirthDay.slice(0,6));
event.value = util.printd("mm/dd/yyyy", BirthDayMonth);
}"
This code is obviously incomplete (or wrong) since we don't know how you set CustomerDOB. Also that's not what an if statement looks like. Did you check the console for errors?
Copy link to clipboard
Copied
CustomerDOB is user entered with mm/dd/yyyy format selected on the properties' of the dateObject field. All of the dateObject fields are set this way.
Copy link to clipboard
Copied
Try this:
var BirthDay = this.getField("CustomerDOB").value;
if(BirthDay == "")
event.value = "";
else {
var BirthDayMonth = util.scand("mm/dd/yyyy", BirthDay.slice(0,6));
event.value = util.printd("mm/dd/yyyy", BirthDayMonth);}
Copy link to clipboard
Copied
Hey Nesa; just wanted to say thank you for your response- it worked! But I also wanted to bring your attention to try67's response regarding the splice comment. Something to keep in mind- thank you Nesa and Be Safe!!!
Copy link to clipboard
Copied
Your code doesn't make much sense. Please explain what you're trying to achieve, first.
Copy link to clipboard
Copied
customer enters their birth date in CustomerDOB dateObject field. A custom calculation script in the BirthDate dateObject field splices the month and day of the user-entered date string and combines it with the current year. For example, the user enters 11/02/1967 in the CustomerDOB dateObjectField and 11/02/2021 is calculated as the value and displayed in the BirthDate dateObject field. I then intend to use this 'currrent' BirthDate value in subsequent calculations to obtain values for different periods of time, applying these resultant time periods to determine fees owed by the customer. Everyday in my job I see how language barriers compound the fear factor of dealing with gov. bureaucracy. My goal is to set up a simple user interface that completes all the appropiate gov forms correctly, avoiding confusion and delays for our customers. Yes, there are still some of us that consider it a privilege to work for the public!
Copy link to clipboard
Copied
OK, I see. Use this script, then:
var BirthDayString = this.getField("CustomerDOB").valueAsString;
if (BirthDayString=="") event.value = "";
else {
var BirthDay = util.scand("mm/dd/yyyy", BirthDayString);
event.value = util.printd("mm/dd/", BirthDay) + new Date().getFullYear();
}
Copy link to clipboard
Copied
This works to reset the dateObject fields, which was my objective; and so does Nesa's solution to bringing the birthdate current, like yours also. Question:
Nesa used .value, and you used .valueAsString. I see you added 'String' to 'BirthDay' in the util.scand line. I remember reading the String () method can be used to get the 'face value of the object'. Can you explain when .value should be used instead of .valueAsString, and confirm whichever is used, the util.scand method must use the similar setup, but the util.printd only uses the .value, never .valueAsString when I'm trying to get a resultant value from a calculation? Thank you try67 for guidance!!!
Copy link to clipboard
Copied
I prefer to use valueAsString when reading the value of a field because then I know what kind of object it returns. With the value property you don't know for sure. Could be a string, or a number, or an array or even a boolean. And the name change has nothing to do with the String() constructor. What matters is the change in the property accessed.
I changed your code so that if the DOB is empty the result will be empty too. This is what was causing the error you were having, because scand will return the current date if the string supplied to it is empty.
I also changed your code to not use the splice method since it's not reliable in this case. If the user entered "1/1/1980", for example, instead of "01/01/1980" (which the "mm/dd/yyyy" format allows them to do), your original code would have failed entirely.
Copy link to clipboard
Copied
His code won't fail if you enter 1/1 because code is not writed for that format in the first place,code is writed to include 01/01 so there is nothing wrong with the code if you use it correctly.
Copy link to clipboard
Copied
You're wrong. It does produce an error. Here's what happens in the original file when I enter "1/1/1989" into the "CustomerDOB" field:
The date is formatted to look like "01/01/1989" but the actual value is still "1/1/1989" so the splice method returns an incorrect value, which then returns null when trying to convert it to a Date object with scand, and printd throws an error.
Copy link to clipboard
Copied
Yes, because code is not meant for that format, if you change slice(0,4) it will work,It's not for 1/1/1980 format.
Well if you want to be tehnical use your code and enter 02/29/1980 for example
Copy link to clipboard
Copied
Yes, because code is not meant for that format
By @Asim123
Yes, and that's exactly the problem with it. You have to take into account things the end-user can do, even if they're not what you intend them to do... As long as they can do it, I can assure you they will do it.
Well if you want to be tehnical use your code and enter 02/29/1980 for example
By @Asim123
That is an extremely narrow issue, and a very unlikely one, but you're right that technically it will produce an incorrect result. I've adjusted the script to take care of that, too:
var BirthDayString = this.getField("CustomerDOB").valueAsString;
if (BirthDayString=="") event.value = "";
else {
var BirthDay = util.scand("mm/dd/yyyy", BirthDayString);
BirthDay.setFullYear(new Date().getFullYear());
event.value = util.printd("mm/dd/yyyy", BirthDay);
}
Copy link to clipboard
Copied
Yea you are right about that, thats why it's best to stick to what OP wants cuz either way you can't use every little thing into account, for example not his and not yours code deals with 29th February correctly, it will just transfer it to March 1st and that is not what OP wanted, although it's very unlikely he will evers use 29th but still it's possibility.
Copy link to clipboard
Copied
Changing it to March 1st is the correct way of doing it. If they want it to work differently they will need to specify in what way, but I consider the latest code I posted to provide the correct answer to this question.
Copy link to clipboard
Copied
Mmm that can't be right, OP wants date of birth (mm/dd+current year) so if someone is born on 29th February changing his day of birth to March 1st is NOT correct.
Copy link to clipboard
Copied
So what is the correct outcome, in your opinion, in that scenario?
In computing terms, this is correct. It might not be what you'd expect, though.
Copy link to clipboard
Copied
From what OP wants correct output would be 02/29/2021 from there it all depends how he uses it for future calculations, I'm just saying what OP asked for.
Copy link to clipboard
Copied
Since there's no such date I bet that's not what they want... And if they did, my original script would give them that. Anyway, this discussion has run its course.
Copy link to clipboard
Copied
Wow! so much to consider!! Thank you!!! I'm using your sugggested script and I have modified it for my next calculation that occurs in the AdvancedRenewalPeriodDate dateObject field. I have the following script, but various changes I have incorporated into it have continually produced today's date rather than the BirthDate dateObject value minus 90 days, which is the calculation in the custom calc property for the AdvancedRenewalPeriodDate dateObject field. I'm confused because the BirthDate dateObject field will always contain a value (the customer's date of birth corrected to the current year) so it should'nt display the default system clock value (today's date). Guidance?
My most promising two attempts:
One attempt:
var BirthDayString = this.getField("BirthDate").valueAsString;
if (BirthDayString=="") event.value = "";
else {
var BirthDay = util.scand("mm/dd/yyyy", BirthDayString);//current year date of birth
var ARP = new Date();
ARP.setDate(BirthDay.getDate() - 90);
var dARP = util.scand("mm/dd/yyyy", ARP);
event.value = util.printd("mm/dd/",dARP) + new Date().getFullYear();
}
another attempt:
var BirthDayString = this.getField("BirthDate").valueAsString;
if (BirthDateString=="") event.value = "";
else {
var BirthDay = util.scand("mm/dd/yyyy", BirthDayString);
var ARP = new Date();
ARP.setDate(BirthDay.getDate() - 90);
var dARP = util.scand("mm/dd/yyyy", ARP);
event.value = util.printd("mm/dd/yyyy",dARP);
}
Copy link to clipboard
Copied
The phantom date is the result of the calculation at this field.
Copy link to clipboard
Copied
yes, it's populating the system's clock date as default value in the BirthDate dateObject field when nothing is user-entered into the CustomerDOB dateObject field. Thank you Bernd for your response!