Date field and day field. Day field is not accurate...Help!

New Here ,
Jun 28, 2017

Copy link to clipboard

Copied

Hi guys, I'm currently using the following script for my day

// get the date from the "Date1"
var d = new Date(this.getField("Date1").value);
if (!isNaN(d.getDay()))
{
switch (d.getDay())
{
case 0:
event.value = "Sun";
break;
case 1:
event.value = "Mon";
break;
case 2:
event.value = "Tue";
break;
case 3:
event.value = "Wed";
break;
case 4:
event.value = "Thu";
break;
case 5:
event.value = "Fri";
break;
case 6:
event.value = "Sat";
break;
default:
event.value = "";
}
}
else
{
event.value = "";

}

My problem here is that for exemple.. today the 28th says TUESDAY not Wednesday... I'm extremely confused. Any ideas?

Adobe Community Professional
Correct answer by Joel_Geraci | Adobe Community Professional

Assuming the value of your field is a string, you need to parse it into a Date object using something like the code below. Adjust for the format of your date string.

var d= util.scand("mm/dd/yyyy", this.getField("Date1").value);

TOPICS
Acrobat SDK and JavaScript, Windows

Views

144

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Date field and day field. Day field is not accurate...Help!

New Here ,
Jun 28, 2017

Copy link to clipboard

Copied

Hi guys, I'm currently using the following script for my day

// get the date from the "Date1"
var d = new Date(this.getField("Date1").value);
if (!isNaN(d.getDay()))
{
switch (d.getDay())
{
case 0:
event.value = "Sun";
break;
case 1:
event.value = "Mon";
break;
case 2:
event.value = "Tue";
break;
case 3:
event.value = "Wed";
break;
case 4:
event.value = "Thu";
break;
case 5:
event.value = "Fri";
break;
case 6:
event.value = "Sat";
break;
default:
event.value = "";
}
}
else
{
event.value = "";

}

My problem here is that for exemple.. today the 28th says TUESDAY not Wednesday... I'm extremely confused. Any ideas?

Adobe Community Professional
Correct answer by Joel_Geraci | Adobe Community Professional

Assuming the value of your field is a string, you need to parse it into a Date object using something like the code below. Adjust for the format of your date string.

var d= util.scand("mm/dd/yyyy", this.getField("Date1").value);

TOPICS
Acrobat SDK and JavaScript, Windows

Views

145

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 28, 2017 0
Adobe Community Professional ,
Jun 28, 2017

Copy link to clipboard

Copied

Assuming the value of your field is a string, you need to parse it into a Date object using something like the code below. Adjust for the format of your date string.

var d= util.scand("mm/dd/yyyy", this.getField("Date1").value);

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 28, 2017 1
New Here ,
Jun 28, 2017

Copy link to clipboard

Copied

Thanks guys finaly got this working!!! Im in no way a java scripter so this helped tremendously.

Again thank you!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 28, 2017 0
Adobe Community Professional ,
Jun 28, 2017

Copy link to clipboard

Copied

I would set my entry date field format to one of the standard date formats and use the util.scand method as mentioned to create a date object. You can then simplify your code by using the util.printd method to format the 3 character day of the week from the date object.

Your script with error detection could be:

// get the date from the "Date1"

var oDateField = this.getField("Date1");

if(oDateField == null) {

app.alert("Error accessing Date1 field", 0,1, "Field Error");

} else {

if(oDateField.valueAsString == ""){

  event.value = "";

} else {

   var oDate = util.scand("dd-mmm-yyyy", oDateField.valueAsString);

  if(oDate == null) {

   app.alert("Date conversion error " + oDateField.valueAsStirng, 0,1, "Date Conversion Error");

  } else {

   event.value = util.printd("ddd", oDate);

  } // end date object null;

} // end field value not null;

} // end date field not null;

This code catches bad field names and incorrect date strings. You will need to adjust the date format for the util.scand method to match the input field's date format.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 28, 2017 2