
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-31-2017 08:41 AM
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.
Davide.
Solved! Go to Solution.
- Labels:
-
Team Development
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-31-2017 10:11 AM
Hi Davide,
Interesting we don't seem to have any methods available to easily get a handle to these values, however if you want a quick answer (considering its almost close of business on a Friday), you could use the getValue() method.
This returns a String of the date and time value stored by the GlideDateTime object in the internal format, yyyy-MM-dd HH:mm:ss, and importantly the system time zone, UTC by default.
From this you could use the String methods available to get a handle to these values. Foe example the split method and/or substring method etc.
Not the cleanest of solutions but I'll take a look later for you.
Please mark this response as correct and helpful via your post link (GlideDateTime - get hours, minutes and seconds ) to help others with the same question.
Thanks,
Robbie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-31-2017 09:12 AM
Hi Davide,
Is this a display issue such as converting a duration value such as '23:24:41' to something that would read 23 hours, 24 minutes and 41 seconds ? Or are you wanting to get a seperate 'hande' to these values?
Have you tried leveraging the javaScript Date object? The below should help you out...
(You can run this as background script to test if you want to play - assuming youre in Dev)
var currentTime = new Date();
var year = currentTime.getFullYear();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
gs.print('currentTime :' + currentTime);
gs.print('hours :' + hours);
gs.print('minutes :' + minutes);
gs.print('seconds :' + seconds);
Please mark this response as correct and helpful via your post link (GlideDateTime - get hours, minutes and seconds ) to help others with the same question.
Thanks,
Robbie

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-31-2017 09:22 AM
my code is this, but dont work extrating hour,minutes and second
var gdt = new GlideDateTime(current.sys_created_on); | |
gr.u_opened = current.sys_created_on; | |
gr.u_opened_year = gdt.getYearUTC(); | |
gr.u_opened_month = gdt.getMonthUTC(); | |
gr.u_opened_day = gdt.getDayOfMonthUTC(); | |
gr.u_opened_hour = gdt.getHours(); | |
gr.u_opened_minute = gdt.getMinutes(); | |
gr.u_opened_second = gdt.getSeconds(); |
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-31-2017 09:57 AM
Hi Davide,
Sorry, I misled you a little. The getHours() etc methods are from the Date object, not the GlideDateTime object.
Let me take a look for you.
Thanks,
Robbie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-01-2022 09:18 AM
Please note here timing are coming from broswer time not from the servicenow timing