The CreatorCon Call for Content is officially open! Get started here.

How to get current date and time based on users timezones

mdsannavulla
Kilo Guru

Hi All,

In our instance my time zone is US Eastern so when I am getting any date and time field value it is giving in US eastern. But my systems time zone is India so when I used "new Date()" it is giving value in India's timezone. So I am not able to compare those two field values.Please see the below code I have written.

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

  if (isLoading || newValue == '') {

  return;

  }

var stDate=new Date(getDateFromFormat(g_form.getValue('RequestedStartDate'),g_user_date_time_format));

var now = new Date();

  //alert(stDate);

  //alert(now);

  if(stDate < now)

  {

        alert('System open time should be greater than or equal to current date and time');

  g_form.clearValue('RequestedStartDate');

  //return false;

  }

}

9 REPLIES 9

mdsannavulla
Kilo Guru

Hi All,



Any idea on this?


Hi Md


just generate your now variable in the same way, instead of reading a form value, pass the current date / time


You mean to say get the value of current date and time from the form by creating another date and time field right?


Nope, but I had a quick look at one of my scripts for a Time worked mandatory field


i have that compares the sys_updated_on field against the current date and time


It uses a couple of ajax calls



function timeWorkedMandatory()


{


      var lsu = g_form.getValue('sys_updated_on');


      g_form.setVisible('sys_updated_on',false);


      var state = g_form.getValue('state');


      if (state !=7)


      {    


              var ajax = new GlideAjax('MyDateTimeAjax');


              ajax.addParam('sysparm_name','nowDateTime');


              ajax.getXMLWait();    


              var rightnow = ajax.getAnswer();


              //var rightnow = getDateFromFormat(ajax.getAnswer(), g_user_date_time_format);


           


                   


              var ajaxdc = new GlideAjax('ClientDateTimeUtils');


              ajaxdc.addParam('sysparm_name','getNowDateTimeDiff');


              ajaxdc.addParam('sysparm_fdt',lsu);


              ajaxdc.addParam('sysparm_difftype','second');    


              ajaxdc.getXMLWait();


              var answer = ajaxdc.getAnswer();


              //g_form.addInfoMessage('rightnow : ' + rightnow);


              //g_form.addInfoMessage('lsu : ' + lsu);


              //g_form.addInfoMessage('answer : ' + answer);


                                                       


           


           


           


           


              if(g_user.hasRole('TimeMandatory') && answer < -60 )


              {    


                      g_form.setVisible('u_time_worked',true);


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


                      g_form.setMandatory('u_time_worked',true);


              }


              else if(g_user.hasRole('TimeMandatory') && answer > -60)


              {    


                      g_form.setVisible('u_time_worked',true);


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


                      g_form.setMandatory('u_time_worked',false);


              }


              else            


              {    


                      g_form.setVisible('u_time_worked',false);


                  g_form.setMandatory('u_time_worked',false);


              }



      }


}