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-21-2023 11:54 PM - edited ‎10-21-2023 11:54 PM
Hi @subhadeep1618 ,
Certainly! To add or subtract hours to a GlideTime variable in a scripting context, you can follow these steps:
1. **Convert GlideTime to GlideDateTime:**
GlideTime does not support direct arithmetic operations. Convert your GlideTime to a GlideDateTime object.
var gd = new GlideDateTime();
gd.setDisplayValue(start_time); // Assuming start_time is a string representing the time
2. **Perform Addition/Subtraction:**
After converting to GlideDateTime, you can easily add or subtract hours using the `add()` and `subtract()` methods.
To add 5 hours:
gd.addHours(5);
To subtract 5 hours:
gd.subtractHours(5);
Now, `gd` contains the modified time based on your addition or subtraction.
3. **Convert Back to GlideTime (if needed):**
If you need the result back in the GlideTime format, you can convert it back.
var actual_start_time = gd.getLocalTime(); // Assuming you want to convert it back to local time
Remember, this process involves working with the GlideDateTime object, which allows you to perform various date and time operations.
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-22-2023 12:36 AM
Hi @Danish Bhairag2 ,
Thanks for your response.
But start_time is not a string, its value of GlideTime type.
I will fetch it from below table.
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 12:45 AM
Hi @subhadeep1618 ,
Ok so just remove that line gd.setDisplayValue & instead just pass ur start_time like below n please check
var gd = new GlideDateTime(start_time);
Then carry on with rest of the code.
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-22-2023 02:46 AM
Hi @Danish Bhairag2 ,
Again, thanks for your quick reply back.
But sadly, this is not working.
gd.addHours(5) is actually returning an undefined value.
But I may have found an alternate solution, which is working as per my requirement.
In next few days, I will document it and post it as a solution in this thread with my use case scenario.
Please mark this post as a solution and also as helpful, if this resolves your issue or query.
Thanks,
Subhadeep Ghosh.