Time gets saved as 00:00:00 in date/time field

Ankita3
Mega Expert

Hello guys,

Can anyone please help??!!

We have this field on our instance "contact_date" which is date/time field which captures the current date and time (client scripts are designed for the same)once the choice is selected from the drop down field (u_contact_sucessfull).

Now the issue is:

1. I select the option from the drop down (e.g. Phone)

2. Contact date gets populated (form is not yet saved)

3. save the form.

4. check the field if the date and time is properly captured, but Date stays and times turns 00:00:00.

Script to populate the time and date is as:

**** onChange Client script ****

if (g_form.getValue('u_contact_date') == '') {

    var d =new Date();

                  var navigator=window.navigator.appName;

                  if (navigator == 'Microsoft Internet Explorer') {

                      // var d =new Date();

  var now = gr.nowDateTime();

                        var day = d.getDate();

                        var month = d.getMonth() + 1;

                        var year = d.getFullYear();

                        //g_form.setValue('u_contact_date', day+"-"+month+"-"+year +" "+d.toLocaleTimeString());

    g_form.setValue('u_contact_date', now);

                  } else {

          var strDate = replaceAll(d.toLocaleString(),'/', '-');

        g_form.setValue('u_contact_date', strDate);

                  }

    }

   

function replaceAll(txt, replace, with_this) {

  return txt.replace(new RegExp(replace, 'g'),with_this);

}

Can anyone please suggest what's going wrong.? Help is much appreciated.!!

Ankita.

10 REPLIES 10

Ankita3
Mega Expert

Hi both,



My original code is like:



if (g_form.getValue('u_contact_date') == '') {


    var d =new Date();


                  var navigator=window.navigator.appName;


                  if (navigator == 'Microsoft Internet Explorer') {


                        var d =new Date();


                        var day = d.getDate();


                        var month = d.getMonth() + 1;


                        var year = d.getFullYear();


                        g_form.setValue('u_contact_date', day+"-"+month+"-"+year +" "+d.toLocaleTimeString());


                  } else {


          var strDate = replaceAll(d.toLocaleString(),'/', '-');


          g_form.setValue('u_contact_date', strDate);


                  }


    }



function replaceAll(txt, replace, with_this) {


  return txt.replace(new RegExp(replace, 'g'),with_this);


}



"now" was something I was trying solve.



Ankita


Chuck Tomasi
Tera Patron

Date/time manipulation on the client side can be a bit of a trick. It's pretty trivial if you use a business rule and save that value on submit (insert=true, update=true)



Condition: current.u_contact_date.nil()


Script:



current.u_contact_date = gs.nowDateTime();


Yes probably. but the same client script also does mandatory/Display work in the same script.



if (newValue != '') {


    g_form.setMandatory('u_contact_unsucessfull', false);


    g_form.setValue('u_contact_unsucessfull', '');


                  g_form.setDisplay('u_contact_unsucessfull', false);


    } else {


          g_form.setDisplay('u_contact_unsucessfull', true);


    }



Hence we have the date/time calculation and Display/Mandatory stuff together.



Ankita


Change of approach. I used the date/time from the server via a GlideAjax call.



Client script (substitute your field names accordingly)



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading)


      return;



  if (g_form.getValue('status') == 'Silver') {


      var ga = new GlideAjax('MyDateUtils');


      ga.addParam('sysparm_name', 'currentDate');


      ga.getXML(processResponse);


  }


}


function processResponse(response) {


  var answer = response.responseXML.documentElement.getAttribute("answer");


  g_form.setValue('u_glide_date_time_1', answer);


}



Script include (MyDateUtils):



var MyDateUtils = Class.create();


MyDateUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {


  currentDate : function() {


      return new GlideDateTime();


  },



  type: 'MyDateUtils'


});


Hello there,



I tried the approach you have suggested.


1. Changes done for CS.


2. Script Include created.



No, it still gives 00:00:00.



Ankita