- 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 07:57 AM
what are you using? can you show the full script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2024 08:03 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2024 08:07 AM
Im getting right result
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2024 08:11 AM
I wonder if this is because of timezone ? What time zone is your user profile in?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2024 08:16 AM
it is in cet.
but i have some logics to be written based on hour. If the hour is not coming properly, then how do i execute the scripts.