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

Hi @lucky24,

 

Can you confirm when you are checking both the native and portal UI's that you're using the same user profile (which has the same timezone settings)?

 

Time is a common aspect where people get confused.

Time is relative to the user's location. The difference between US East Coast time and India (Mumbai) is 10.5 hours (currently). Therefore it is impossible to set the date time to be exactly the same in both locations. 

This is why IT systems and ServiceNow store time in UTC (Coordinated Universal Time).

I would set (and it is best practice) to set the time in UTC which is by default and ServiceNow will display this time relative to a user's timezone.

 

I've removed the timezone setting line from your original script. Use the below:

 

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

 

@lucky24 - 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

dhanrajb
Tera Guru

Hi @lucky24 ,

 

Try the below logic

 

 

var myDate = new GlideDateTime();//Replace with your date field name.
var daysCount = myDate.getDaysInMonthUTC();
var newDate= daysCount +'-'+myDate.getMonthUTC()+'-'+myDate.getYearUTC()+" "+"23:59:00";
var newDateTime = new GlideDateTime(newDate)
gs.info(newDateTime);

 

 

-Dhanraj.

Robbie
Kilo Patron
Kilo Patron

Hi @lucky24,

 

How did you go with the updated script I provided yesterday?

Please also take note of the advice I gave around time zones.

 

@lucky24 - Can you please mark my solution if it works and solves your question.

Community members like myself take time

out of their day to help and providing feedback not only helps other people in the community, it recognises the contribution members make.


Updated Script:

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 = newGlideDateTime(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