Select to view content in your preferred language

How do you set a date field value using Javascript?

1174
1
01-24-2014 12:12 PM
DanielOtt
New Contributor
I'm trying to write javascript code to set a date field value during a form's onOk event.  I'm running into a type mismatch error.  I'm using the following code:

function myEvent_onOk() {
  var oFields, oDate;
  oFields = ThisEvent.Object.Fields;
  oDate = new Date();
   
  oFields("CREATED_ON").Value = oDate.toString();  // I've also tried oFields("CREATED_ON").Value = oDate;
}

When I perform a typeof(oFields("CREATED_ON").Value), the typeof operator returns a type date, which AFAIK isn't a typical javascript type.  Doing a typeof(oDate) returns type object. 

Any ideas how to get around this? 

Thanks,
Daniel
Tags (3)
0 Kudos
1 Reply
DanielOtt
New Contributor
I got this issue resolved by following the last JScript example under the section heading
'>Initializing a DateTime control
.

My code ended up being this for my form's OnOk event:

function form_onOk() {
    var objForm = ThisEvent.Object;
    var dtDate = new Date();
    
    if (objForm.Fields("CREATED_ON").IsNull) {
        objForm.Fields("CREATED_ON").Value = dtDate.getVarDate();
    } else {
        objForm.Fields("UPDATED_ON").Value = dtDate.getVarDate();
    }    
}


(A thank you to Mark for pointing this out to me!)
0 Kudos