Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Set last day of month and time 23:59:00

lucky24
Tera Contributor

Hi Team,

 

we have one field called due date in which we have to populate the last day of the month from the pre-hire field and the time should be 23:59:00 which should according to America/new_york time zone

 

suppose in the pri-hire field we have (10/02/2024) so in the due date field I want populate 29/02/2024 23:59:00 according America/new_york

I am not getting logic with script.

I am working on scoped application.

 

Thanks

7 REPLIES 7

Robbie
Kilo Patron
Kilo Patron

Hi @lucky24,

 

Working with dates and times can always be fun. You need to do something along the lines of the below.

Can you confirm if this to happen on change - when the pre-hire field is populated for example?

 

//Get the value from the field
var preHireDateTimeVar = g_form.getValue('your_prehire_field') //Assume this is a date/time field
//Cast the value to a type of GlideDateTime for manipulation
var gdt = new GlideDateTime(preHireDateTimeVar);
gdt.setDayOfMonth(32); // set the value greater than 31. Even for months such as February, you will always get the last day of the month
//Set the time element
var newTime = gdt.toString().slice(0,10) + " 23:59:59";
//Cast the value to a type of GlideDateTime
var gdtNewTime = new GlideDateTime(newTime);
//Set the date/time
g_form.setValue("yourDueDateField", gdtNewTime);
 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Helpful.

 

Thanks, Robbie

lucky24
Tera Contributor

Hi Robbie,

Thanks for your response, I tried with business rules and written code but it is not working.

Could you please check where I am making mistake.

 

var eom = new GlideDateTime();
 eom.setValue(current.parent.subject_person.u_onboarding_hire_date);
eom.setTimeZone("America/New_York");
eom.setDayOfMonthUTC(32);
var arrDate = [];
arrDate = dueDate.toString().split(' ');
    //current.due_date = eom;
current.due_date= (arrDate[0]+ ' 23:59:59','yyyy-MM-dd HH:mm:ss');

 

 

Hi @lucky24,

 

I've tweaked your script a little. Try the below.

Questions I had when reviewing the code: Why do you need the array arrDate? Anyway, the below works, I've tested it, and should give you what you want.

 

var eom = new GlideDateTime();
eom.setValue(current.parent.subject_person.u_onboarding_hire_date);
eom.setTimeZone("America/New_York");
eom.setDayOfMonthUTC(32);
var newTime = eom.toString().slice(0,10) + " 23:59:59";
var gdtNewTime = new GlideDateTime(newTime);
current.due_date = gdtNewTime;
 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Helpful.

 

Thanks, Robbie

lucky24
Tera Contributor

Hi Robbie,

Thanks for the solution.

When I created a record from the India (IST) time zone it was working expected but when I created a record  from America/los_Angeles time zone, in native view it showed the correct value but in portal, it is setting the next month of the first day 

 

lucky24_0-1708413683354.png