Formatting Dates in JavaScript on a Form

  • 27 September 2016
  • 7 replies
  • 9 views

Userlevel 4
Badge +10

Hi Folks,

 

I am just getting my feet wet in Forms and JS. I am trying to make the following function run against a form button click to fill out some fields in a form.

 

function signFormPM() {
NWF$('#'+ jsvar_PMDate).val(fn-FormatDate(Current Date, MM/dd/yyyy));
NWF$('#'+ jsvar_PMName).val("Last NameFirst Name (Current User)");
}

The name works ok but the date is loading a decimal into the text field. Can anyone assist?

 

192752_pastedImage_1.png

 

Thanks!


7 replies

Userlevel 4
Badge +10

I moved this over to Dev Talk since it dealt with JavaScript.

Userlevel 5
Badge +14

try to enclose date formula into apostrophes like

NWF$('#'+ jsvar_PMDate).val("fn-FormatDate(Current Date, MM/dd/yyyy)");

I'm not sure why do you get a number there, but in my env. your formula didn't work at all.

the reason is that fn-FormatDate() inline function is evaluated at render time and my env. is setup with spaces in date. so those space broke up javascript code.

could be that in your env. the output is evaluated as a math expression and so you see result of 3 digits divided.

Userlevel 4
Badge +10

Hi Marian,

When I added double quotes or single quotes around the function, I got the literal string of the function populating my text box.

192952_pastedImage_1.png

Thanks,

Patrick

Badge +11

Hi ,

I verified your formula and indeed it saved it as a decimal. I'm not sure why and haven't been able to find the formula to put the literal value. Hopefully someone from can point us in right direction.

Userlevel 4
Badge +10

Hi Kapil Joshi‌,

I worked with it some more and it turns out that Marian Hatala‌'s solution worked for me. Not sure why it did not work the first time I tried it. Perhaps I was troubleshooting too many issues at once on the form.

//this line renders a deciaml:
    NWF$('#'+ jsvar_PMDate_txt).val(fn-FormatDate(Current Date, MM/dd/yyyy));

//This line renders the propper date format:
    NWF$('#'+ jsvar_PMDate_txt).val("fn-FormatDate(Current Date, MM/dd/yyyy)");

Here is where and how it rendered...

192986_pastedImage_1.png

Badge +11

Great. Good to know!!! happy.png

Userlevel 5
Badge +14

I've tested my assumption of date being evaluated as math formula and it really seems to be so

Reply