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.

How to get day and month, Year

Sironi
Kilo Sage

Hi,

please help me in Date , it could not getting Month and year. displaying undefined 

Business Rule: Incident  , Update, Before

var cdate=current.u_date_needed;
var d = new Date();
var hh = d.getHours();
var date=0;
gs.addInfoMessage(gs.getMessage(hh));---------------------getting correct local user hours -------
var dd = String(d.getDate()).padStart(2, '0');
var mm = String(d.getMonth() + 1).padStart(2, '0'); 
var yyyy = d.getFullYear();
var today=yyyy+'-'+mm+'-'+dd;
gs.addInfoMessage(gs.getMessage(today));         -------------2019-undefined-undefined
gs.addInfoMessage(gs.getMessage(cdate));
if(today>=cdate)
{
if (hh > 7)
{
current.state='Closed Incomplete';
}
}

16 REPLIES 16

SanjivMeher
Mega Patron
Mega Patron

Try this

 

var cdate=current.u_date_needed;
var d = new Date();
var hh = d.getHours();
var date=0;
gs.addInfoMessage(gs.getMessage(hh));
var dd = String(d.getDate());
if (dd.length=1)
dd='0'+dd;
var mm = String(d.getMonth() + 1);
if (mm.length=1)
mm='0'+mm;
var yyyy = d.getFullYear();
var today=yyyy+'-'+mm+'-'+dd;
gs.addInfoMessage(gs.getMessage(today));
gs.addInfoMessage(gs.getMessage(cdate));
if(today>=cdate)
{
if (hh > 7) 
{
current.state='Closed Incomplete'; 
}
}


Please mark this response as correct or helpful if it assisted you with your question.

please help me in Hours calculation 

var d = new Date();   // it takes local Time right?
var hh = d.getHours();

gs.addInfoMessage(gs.getMessage(hh));

in my local  location current time is 11:14 PM (23 :14 min) but here it is displaying hours is 10 why? , actually i guess it should display 23 right ?

can you suggest me for right track ?

find_real_file.png

where was wrong in script

Can you try this instead.

 

var cdate=current.u_date_needed;

var gdt = new GlideDateTime();
var today = gdt.getLocalDate();
var hh = gdt.getLocalTime().getByFormat('hh');

gs.addInfoMessage(gs.getMessage(hh));
gs.addInfoMessage(gs.getMessage(today));
gs.addInfoMessage(gs.getMessage(cdt));

if(today>=cdate)
{
if (hh > 7) 
{
current.state='Closed Incomplete'; 
}
}


Please mark this response as correct or helpful if it assisted you with your question.

when i used same script in OnChange it is giving right date and time

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

dateneeded();
function dateneeded()
{
var cdate=g_form.getValue('u_date_needed');
var d = new Date();
var hh = d.getHours();
var date=0;
alert(d);

find_real_file.png