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

Hi Pranesh,

 

i tried below script it is also giving hours, not sure is it correct or not.

please help me, is Script 2 giving right value or not ?

 

Script 1: you proposed 

var gdt = new GlideDateTime();
var gt = gdt.getTime();
gs.info("Hours :"+gt.getHourLocalTime());

Result : Hours:10

 


Script 2 :  i tried it 

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

Result : DayOf Time Now :Fri Feb 19 2021 10:01:11 GMT-0800 (PST)-------10

You should always try to use the servicenow's documented APIs, so go with GlideDateTime instead of date

Thanks Pranesh, thanks for suggesting here for Best Practice .

i have used below format

var gdt = new GlideDateTime();
var gt = gdt.getTime();
gs.info("Hours :"+gt.getHourLocalTime());

@chanikya See my reply above, I cover various ways which you should do this using the platform APIs. Please do not use the javascript Date object on the platform, there is absolutely no need.

this should clear your all doubts


var gdt = new GlideDateTime();
gs.info(gdt);                    //UTC
gs.info(gdt.getDisplayValue());  //local instance time
gt = gdt.getTime();
gs.info(gt.getHourLocalTime());   // The hours using the local time zone. The number of hours is based on a 12 hour clock. Noon and midnight are represented by 0, not 12.
gs.info(gt.getHourOfDayLocalTime()); //The hours using the local time zone. The number of hours is based on a 24 hour clock.
gs.info(gt.getHourOfDayUTC());      //The hours using the UTC time zone. The number of hours is based on a 24 hour clock.