GlideDateTime - get hours, minutes and seconds

Ravi Gadipudi
Kilo Contributor

Hi all

How Can I extract hour, minute and seconds from DataTime field?

for example, I'm trying to write this business rule

var gdt = new GlideDateTime ("2017-03-31 15:13:43");

year = gdt.getYearUTC();

month = gdt.getMonthUTC();

day = gdt.getDayOfMonthUTC();

hour =

minute =

second =

Thanks,

Best Regards.

Ravi

 

3 REPLIES 3

Shishir Srivast
Mega Sage

I don't think we have OOB API to extract those values, you can try to use.

 

var gdt = new GlideDateTime ("2017-03-31 15:13:43");
var gdtsplit = gdt.getDisplayValue().split(' ')[1].split(':');
var hour = gdtsplit[0];
var minute = gdtsplit[1];
var second = gdtsplit[2];

 

SanjivMeher
Mega Patron

Try this

var gdt = new GlideDateTime("2017-03-31 15:13:43");

var timeSection = gdt.toString().split(' ')[1]; //Gets the Time

year = gdt.getYearUTC();

month = gdt.getMonthUTC();

day = gdt.getDayOfMonthUTC();

hour = timeSection.split(':')[0];

minute = timeSection.split(':')[1];

second = timeSection.split(':')[2];

gs.addInfoMessage(hour +'-'+minute +'-'+second);

 

johnboyland
Tera Contributor

Hi Ravi, 

You will need to do this Server side and not client side. 

I would also ensure you use getByFormat to ensure that the format never changes on you ( 24 hours vs 12 hours ) 

 

var gdt = new GlideDateTime("2014-08-31 08:00:00");
var gt = gdt.getTime()
var timeStr = gt.getByFormat('hh:mm:ss');
//var timeStr = gt.getByFormat('HH:mm:ss'); // 24 hour format

var split = timeStr.split(":");
gs.info(split[0]);