How to add or subtract hours from GlideTime value (NOT GlideDateTime or GlideDate)?

subhadeep1618
Tera Guru

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.
6 REPLIES 6

Danish Bhairag2
Tera Sage
Tera Sage

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

 

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.

subhadeep1618_0-1697960123350.png

 


Please mark this post as a solution and also as helpful, if this resolves your issue or query.

Thanks,
Subhadeep Ghosh.

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

 

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.