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

Anurag Tripathi
Mega Patron
Mega Patron

new Date() pics up the date time form browser and not from the server.




on server side if you use .getDisplayValue() on any field, that gives you date time in user's time zone and format.


-Anurag

That comes into string format. Can we convert it into GlideDateTime object and still retain the user's time zone?


This might be what your asking for?

As per:
https://hi.service-now.com/kb_view.do?sysparm_article=KB0594664

-----------------------------------------------------------------------------------------

The setValue() and getValue() methods require parameters in the UTC time zone and internal format. Other formats may return invalid results.

The toString() method returns the same string as the getValue() method.

The setDisplayValue() and getDisplayValue() methods require parameters in the local time zone (user/system) and local format (user/system). Other formats may return invalid results.

//Example: If user’s date time format is MM.dd.yyyy HH:mm:ss

// Do this

var input = “08.05.2015 16:21:47”;
var gdt = new GlideDateTime();
gdt.setDisplayValue(input);

// Not this

var input = “08.05.2015 16:21:47”;
var gdt = new GlideDateTime(input);

 

-----------------------------------------------------------------------------------------

 

-Anders

Your comment is really helpful for me.

Thanks for the feedback - glad to help.

 

-Anders