How to getHours

chanikya
Kilo Sage

Hi,

some one please suggest me how to get Hours only from  new GlideDateTime()).getLocalTime().

all client  systems are running in PST zone.

1 ACCEPTED SOLUTION

Edited the script

 

if (gdt.getDayOfWeekUTC() == 2) //Tuesday 2 7AM PST (-7)
{

	if (gdtH.getHourOfDayUTC() == 14) {//GMT
		email.setSubject("Gentle Reminder for TimeCard Approval of the week starts on " + par1.lastWeek);
		email.addAddress("cc", par1.manager_email, par1.manager_name);
		template.print('Hi ' + par1.user + ',</br></br>This is a gentle reminder, that the Timecards in Submitted State are overdue, for the week starts on, "' + par1.lastWeek + '" in the system. Kindly Approve or Reject the timecards last by 3PM PST. </br></br>');
		template.print('<br/>');
	}
}
if (gdt.getDayOfWeekUTC() == 3) //Wednesday 3 1AM = tuesday 2 6PM PST (-7)
{
	if (gdtH.getHourOfDayUTC() == 1) { //GMT
		email.setSubject("Final Reminder for TimeCard Approval of the week starts on " + par1.lastWeek);
		email.addAddress("cc", par1.manager_email, par1.manager_name);
		template.print('Hi ' + par1.user + ',</br></br>This is a final reminder, that timecard approval for the below Employees are overdue for the week starts on, "' + par1.lastWeek + '" in the system and needs immediate approval. </br></br>');
		template.print('<br/>');
	}
}

View solution in original post

40 REPLIES 40

Kindly help me in this point , how to get Lastweek day number

var gdt = new GlideDateTime(gs.beginningOfLastWeek());
var last_week = gdt.getDate();

i want to get Lastweek date of  the day of week value from 1 to 7. Monday equals 1, Sunday equals 7.

Callum Ridley1
Tera Guru

I'd like to provide one final update on this, and I think it's important. There may be a bug in GlideTime / GlideDateTime, or the documentation on the developer site may need updating to reflect these findings.

It would appear that when you run the following script, the GlideTime object is instantiated with the local time of the user, however to access the local hour you should not use the getHourLocalTime / getHourOfDayLocalTime methods, you should use the UTC alternatives, or the getByFormat method.

var gdt = new GlideDateTime();
gs.info('The date & time in my User Timezone (' + gs.getSession().getTimeZoneName() + ') right now is: ' + gdt.getDisplayValue() + '\n');
gs.info('If we get a GlideTime object by calling the GlideDateTime.getLocalTime method, the methods return as follows:');
var gtFromGDT = gdt.getLocalTime();
gs.info('The GlideTime.getHourOfDayLocalTime method returns ' + gtFromGDT.getHourOfDayLocalTime() + ' which IS NOT the actual local hour');
gs.info('However, the GlideTime.getHourOfDayUTC method returns ' + gtFromGDT.getHourOfDayUTC() + ' which IS the actual local hour');
gs.info('And finally the GlideTime.getByFormat method returns ' + gtFromGDT.getByFormat('HH') + ' which IS the actual local hour\n');

var gt = new GlideTime();
gs.info('If we intantiate a GlideTime by itself without using GlideDateTime, the methods return as follows:');
gs.info('The GlideTime.getHourOfDayLocalTime method returns ' + gt.getHourOfDayLocalTime() + ' which IS the actual local hour');
gs.info('The GlideTime.getHourOfDayUTC method returns ' + gt.getHourOfDayUTC() + ' which IS NOT the actual local hour');
gs.info('And finally the GlideTime.getByFormat method returns ' + gtFromGDT.getByFormat('HH') + ' which IS the actual local hour');

The output of this script is as follows

*** Script: The date & time in my User Timezone (IST) right now is: 20/02/2021 21:51:25

*** Script: If we get a GlideTime object by calling the GlideDateTime.getLocalTime method, the methods return as follows:
*** Script: The GlideTime.getHourOfDayLocalTime method returns 3 which IS NOT the actual local hour
*** Script: However, the GlideTime.getHourOfDayUTC method returns 21 which IS the actual local hour
*** Script: And finally the GlideTime.getByFormat method returns 21 which IS the actual local hour

*** Script: If we intantiate a GlideTime by itself without using GlideDateTime, the methods return as follows:
*** Script: The GlideTime.getHourOfDayLocalTime method returns 21 which IS the actual local hour
*** Script: The GlideTime.getHourOfDayUTC method returns 16 which IS NOT the actual local hour
*** Script: And finally the GlideTime.getByFormat method returns 21 which IS the actual local hour

It seems as though GlideDateTime.getLocalTime simply does something internally analogous to the following script

var gt = new GlideTime();
gt.setValue('**local_time_from_gdt**');

instead of the following which would instantiate the GlideTime correctly.

var gt = new GlideTime();
gt.setDisplayValue('**local_time_from_gdt**');

In summary, it would appear that the safest method to use when getting the Hour of day from a GlideTime object is getByFormat as this consistently returns the correct value.

@chanikya It might be worth reviewing your code to ensure that you have taken this into account, if you do find that your code is fixed as a result of this post, please mark it as the accepted solution so people can see this information as easily as possible.

Callum

This is Great , Fantastic information about Hours functionality .

Could you please help me , what is difference between

var aaa = new GlideDateTime();
var gdtH = aaa.getTime();
var finl = gdtH.getHourOfDayUTC();
gs.info("FinalHr 1 :" + finl);

Result : FinalHr 1 :13 

 

var aaa = new GlideDateTime();
var gdtH = aaa.getLocalTime();
var finl = gdtH.getHourOfDayUTC();
gs.info("FinalHr 2 :" + finl);

Result : FinalHr 2 :5

 

 

var gdtt = new Date();
gs.info("DayOfTime :" + gdtt + "----" + gdtt.getHours());

Result : DayOfTime :Mon Feb 22 2021 05:30:55 GMT-0800 (PST)----5

GlideDateTime.getTime will return a GlideTime object where the internal value is set to UTC.

GlideDateTime.getLocalTime will return a GlideTime object where the internal value is set to the time in the current users time zone.

Personally, I would ignore the getLocalTime method as it will just confuse you, I've raised this with ServiceNow who are investigating.

I hope this is safest process , please correct me if i m wrong here.

var gdt = new GlideDateTime();
var gtFromGDT = gdt.getLocalTime();
gs.info('Hours' + gtFromGDT.getByFormat('HH'));