- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-31-2024 04:19 AM
Hello experts,
i have a date time value '
2024-06-01 08:00:00
I want to extract the hour from it which is 08.(Should return 08)
Can anyone help me how to do it in a script. I am new to ServiceNow Scripting. Thanx in advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-31-2024 04:33 AM
here you go
var gdt = new GlideDateTime ("2017-03-31 15:13:43");
var gdtsplit = gdt.getDisplayValue().split(' ')[1].split(':');
year = gdt.getYearUTC();
month = gdt.getMonthUTC();
day = gdt.getDayOfMonthUTC();
var hour = gdtsplit[0];
var minute = gdtsplit[1];
gs.print('year = '+ year);
gs.print('month = '+ month );
gs.print('day = '+ day );
gs.print('hour = '+ hour );
gs.print('minute = '+ minute );

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-31-2024 08:48 AM
Hi @deepika46
use getValue() instead getDisplayValue() , this will solve your requirement. You will get correct hrs number.
eg:
var gdt = new GlideDateTime ("2024-06-01 08:00:00");
var gdtsplit = gdt.getValue().split(' ')[1].split(':');
var hour = gdtsplit[0];
gs.print(hour)
Thanks,
Harsh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-31-2024 04:22 AM
Hi,
this should work
var gdt = new GlideDateTime ("2017-03-31 15:13:43");
var gdtsplit = gdt.getDisplayValue().split(' ')[1].split(':');
var hour = gdtsplit[0];
gs.print(hour)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-31-2024 04:29 AM
Hello @Anurag Tripathi ,
Thank you so much. If i want to extract the month date as well. For example in the above example, if i want to retrieve 31. Then could plz tell me how to get that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-31-2024 04:33 AM
here you go
var gdt = new GlideDateTime ("2017-03-31 15:13:43");
var gdtsplit = gdt.getDisplayValue().split(' ')[1].split(':');
year = gdt.getYearUTC();
month = gdt.getMonthUTC();
day = gdt.getDayOfMonthUTC();
var hour = gdtsplit[0];
var minute = gdtsplit[1];
gs.print('year = '+ year);
gs.print('month = '+ month );
gs.print('day = '+ day );
gs.print('hour = '+ hour );
gs.print('minute = '+ minute );
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-31-2024 07:41 AM