Skip to main content
Participant
June 28, 2017
Answered

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

  • June 28, 2017
  • 2 replies
  • 503 views

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?

This topic has been closed for replies.
Correct answer Joel Geraci

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);

2 replies

Inspiring
June 28, 2017

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.

Joel Geraci
Community Expert
Joel GeraciCommunity ExpertCorrect answer
Community Expert
June 28, 2017

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);

Participant
June 28, 2017

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

Again thank you!