The Zurich release has arrived! Interested in new features and functionalities? Click here for more

set a datetime variable for current DateTime to use the user's date format

tkh
Kilo Expert

I have a business rule that is used to set a lead time dateTime field based on the risk value.   So if the risk level is a value of say 1 (high) the amount of days lead time to earliest start date is 14, or i f it is lower like say a value of 4 (low) the number of days to earliest start time would be 1 day.   The earliest start date is calculated by the current DateTime + lead time days.   The earliest start date gets entered as yyyy-mm-dd hh:mm:ss.   As it stands this works just fine. . .until we add in a UI Action that evaluates the earliest start time again a start_date to be sure the start_date doesn't come before the earliest start date.

As long as the user's date format uses the yyyy-mm-dd hh:mm:ss format everything works fine, but if the users date format for the start_date is different then the evaluation doesn't work properly.   So I believe I need to set the earliest start date format match the user's format.

My Business Rule code:   (Please note this isn't my code it was created by someone else.)

// Compute lead time based on risk

if (current.risk == "1") {

current.lead_time="14";

} else if (current.risk == "2") {

current.lead_time="8";

} else if (current.risk == "3") {

current.lead_time="1";

} else if (current.risk == "5") {

current.lead_time="0";

}

// Get the current date time when a risk is updated

var gdt1 = new GlideDateTime(gs.nowDateTime());

// check if lead time is set and then add it to current date time

// Compute Earliest Start date = current date time + lead time

gdt1.addDaysLocalTime(current.lead_time);

current.earliest_start = gdt1.getValue();

Everything I have tried so far has failed to change the format.   Any ideas?

1 ACCEPTED SOLUTION

The value "g_user_date_time_format" is client side only.   As for the business rule, change this line



current.earliest_start = gdt1.getValue();


with


current.setDisplayValue("earliest_start", gdt1.getDisplayValue());



I believe that will fix your issue if I understand your issue correctly.



If it does not you are going to need to post a screen shot of what the user sees and some log statements to show what the system is returning.


View solution in original post

7 REPLIES 7

DrewW
Mega Sage
Mega Sage

If you are trying to do this on the Client you can use "g_user_date_time_format" to get the users date/time format to write your client script.   On the Server side I'm reasonably sure you just need to do things like



current.setDisplayValue("start_time", gdt.GetDisplayValue())



and that will do things in the user time zone and format.


I have tried the g_user_date_time_format without any luck.   I have also tried the setDisplayValue but not in the same way you have it.   Can you help me out just a little more with exactly where this would go, or how I should use it in my script?   would it be more like...



current.setDisplayValue("Earliest_Start ", gdt.GetDisplayValue())= gdt1.getValue();



Sorry I am still a bit new to service now.


The value "g_user_date_time_format" is client side only.   As for the business rule, change this line



current.earliest_start = gdt1.getValue();


with


current.setDisplayValue("earliest_start", gdt1.getDisplayValue());



I believe that will fix your issue if I understand your issue correctly.



If it does not you are going to need to post a screen shot of what the user sees and some log statements to show what the system is returning.


Great this mostly worked!!!!!!!!!!!!   If the format uses a dash like "mm-dd-yyyy" but if it it uses anything else like "mm/dd/yyyy" or "mm.dd.yyyy" then it doesn't change to the correct format.