How to add or subtract hours from GlideTime value (NOT GlideDateTime or GlideDate)?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-21-2023 10:45 PM
I have a GlideTime (Time) type field in my table --- this is not GlideDateTime or GlideDate
Say for example, start_time
Now I want to add and subtract hours to the retrieved value (e.g. 5 hours)
That is, I want to do something like:
var actual_start_time = start_time + 5;
OR
var actual_start_time = start_time - 5;
Can someone please tell me, what is the most simplest way this can be done.
Please mark this post as a solution and also as helpful, if this resolves your issue or query.
Thanks,
Subhadeep Ghosh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-22-2023 04:19 AM
Oh sorry to hear that i couldn't help but happy that you figured out yourself. Cheers!
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-22-2023 02:57 AM
If you want to add or subtract hours from a GlideTime value without converting it to GlideDateTime or GlideDate, you can use GlideDuration to represent the time difference and then add or subtract that duration. Here's how you can do it:
// Assuming 'start_time' is your GlideTime field var start_time = gs.timeAsGlideTime("08:30:00"); // Replace with your actual GlideTime value // Create a GlideDuration representing 5 hours var hoursToAdd = new GlideDuration(5 * 60 * 60 * 1000); // 5 hours in milliseconds // Add the duration to the start_time var actual_start_time = new GlideTime(start_time); actual_start_time.add(hoursToAdd); // Subtract the duration from the start_time var actual_start_time_subtract = new GlideTime(start_time); actual_start_time_subtract.subtract(hoursToAdd); gs.info("Original Time: " + start_time); gs.info("Time after adding 5 hours: " + actual_start_time.getByFormat('HH:mm:ss')); gs.info("Time after subtracting 5 hours: " + actual_start_time_subtract.getByFormat('HH:mm:ss'));
For your example
// Assuming 'start_time' is your GlideTime field
var start_time = gs.timeAsGlideTime("08:30:00"); // Replace with your actual GlideTime value
// Convert the GlideTime to seconds (since there are 3600 seconds in an hour)
var start_time_seconds = (start_time.getHourOfDay() * 3600) + (start_time.getMinute() * 60) + start_time.getSecond();
// Add 5 hours (5 * 3600 seconds)
var new_time_seconds = start_time_seconds + (5 * 3600);
// Handle cases where new_time_seconds exceeds a day (86400 seconds)
new_time_seconds = new_time_seconds % 86400;
// Convert the result back to a GlideTime
var actual_start_time = new GlideTime();
actual_start_time.setDisplayValue((new_time_seconds / 3600) + ":" + ((new_time_seconds % 3600) / 60));
gs.info("Original Time: " + start_time);
gs.info("Time after adding 5 hours: " + actual_start_time);