Custom Script - Difference between two DateTime variables

Hello!

I’m trying to implement a Custom Script that will calculate the difference between two
DateTime Recorder Variables in hours, minutes, and seconds.

It returns a String of this format:
Example: “0h:5m:34s”

The Custom Script throws the Exception Handling when run with no errors attached.
Could you help me figure out why it’s not working?
Also, is there a way to debug Custom Scripts?

Here’s the script:

Hi @mneff14,

Could you please show your input and output variables in “Recorder Variables” tab?

Unfortunately not in Actions Flow. A kind of debugging can be performed in Code perspective in Studio according to this guide: Test Custom Action script in WorkFusion Studio.

Also, I see mistakes in your Custom Action syntax, please check according to the Script as Custom Action link.

Thank you for the quick response and useful resources!

I finally learned some Groovy and switched my code to the Code Editor! Now I’m attempting to give calcRuntime() a test run with some pre-made RDateTime variables before I use Recorder Variables.

I’ve got it working up until the getHour() method, and then I get the error message listed at the end.
What data type does getHour() require?

Here is my new code:

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://web-harvest.sourceforge.net/schema/1.0/config" scriptlang="groovy">
    <robotics-flow>
        <robot driver="universal" close-on-completion="true" start-in-private="true">
            <capability name="SEARCH_ALL_WINDOWS" value="true" />
            <script><![CDATA[
                
                import com.workfusion.studio.rpa.recorder.api.internal.representation.*
                import com.workfusion.studio.rpa.recorder.api.*
                import com.workfusion.studio.rpa.recorder.api.types.*
                import com.workfusion.studio.rpa.recorder.api.custom.*
                import static com.workfusion.studio.rpa.recorder.api.RandomValues.CharacterSet.*

                //enableTypeOnScreen()
 
 
 				// Important imports
 				import java.time.*
 				
 				
 				// Test variables
 				now = RDateTime.now()
 				bot_start_time = RDateTime.of(2020, 6, 29, 9, 25, 13, 'America/Chicago') // Year, Month, Day, Hour, Minutes, Seconds, TimeZone
                
                
                def calcRuntime() {
					// Returns a string containing the number of hours, minutes, and seconds between the two DateTimes
					// Example: "<hours>h:<minutes>m:<seconds>s" 
					//          "0h:3m:45s"
					
					// Get the current time's hours, minutes, and seconds
					//def now = RDateTime.now();
					h_now = RDateTime.getHour(now);
					m_now = RDateTime.getMinute(now);
					s_now = RDateTime.getSecond(now);
					
					// Get the start time's hours, minutes, and seconds
					h_start = RDateTime.getHour(bot_start_time);
					m_start = RDateTime.getMinute(bot_start_time);
					s_start = RDateTime.getSecond(bot_start_time);
					
					// Calculate the differences between the hours, minutes, and seconds
					new_runtime_h = (h_now - h_start).ToString();
					new_runtime_m = (m_now - m_start).ToString();
					new_runtime_s = (s_now - s_start).ToString();
					
					// Return a new string containing the new runtime
					runtime_str = (new_runtime_h + 'h:' + new_runtime_m + 'm:' + new_runtime_s + 's').ToString();
				}
                
                
                // Test the code
                calcRuntime()
                println runtime_str
                
 
            ]]></script>
        </robot>
    </robotics-flow>
    <export include-original-data="true"/>
</config>

This is the error message:

10:54:16 [INFO] -------------------------------------------
10:54:16 [INFO] EXECUTION FAILED
10:54:16 [INFO] No signature of method: static com.workfusion.studio.rpa.recorder.api.types.RDateTime.getHour() is applicable for argument types: (com.workfusion.studio.rpa.recorder.api.types.RDateTime) values: [2020-06-29T10:54:14-05:00[America/Chicago]] Possible solutions: getHour(), getYear(), getZone(), getMonth(), getNano(), getAt(java.lang.String)
10:54:16 [INFO] (Calculate Runtime.xml:53)
10:54:16 [INFO] -------------------------------------------

Hi @mneff14,

Actually, it does not require any type. Your implementation is incorrect. If you would like to find hours, minutes and seconds from the current DateTime variable, use the variable itself, not RDateTime type.
This should work:

h_now = now.getHour();
println h_now; // checking output in Console
1 Like

Oh, of course! That make so much sense! Thank you very much!

Here is the working code now:

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://web-harvest.sourceforge.net/schema/1.0/config" scriptlang="groovy">
    <robotics-flow>
        <robot driver="universal" close-on-completion="true" start-in-private="true">
            <capability name="SEARCH_ALL_WINDOWS" value="true" />
            <script><![CDATA[
                
                import com.workfusion.studio.rpa.recorder.api.internal.representation.*
                import com.workfusion.studio.rpa.recorder.api.*
                import com.workfusion.studio.rpa.recorder.api.types.*
                import com.workfusion.studio.rpa.recorder.api.custom.*
                import static com.workfusion.studio.rpa.recorder.api.RandomValues.CharacterSet.*

                enableTypeOnScreen()
                
                
 				
 				// Test variables
 				now = RDateTime.now()
 				//bot_start_time = RDateTime.of(2020, 6, 30, 9, 25, 13, 'America/Chicago') // Year, Month, Day, Hour, Minutes, Seconds, TimeZone
 				bot_start_time = RDateTime.fromCanonical('2020-06-30T09:25:59-05:00[America/Chicago]')
                
                
                def calcRuntime() {
					// Returns a string containing the number of hours, minutes, and seconds between the two DateTimes
					// Example: "<hours>h:<minutes>m:<seconds>s" 
					//          "0h:3m:45s"
					
					// Get the current time's hours, minutes, and seconds
					//def now = RDateTime.now();
					h_now = now.getHour();
					m_now = now.getMinute();
					s_now = now.getSecond();
					
					// Get the start time's hours, minutes, and seconds
					h_start = bot_start_time.getHour();
					m_start = bot_start_time.getMinute();
					s_start = bot_start_time.getSecond();
					
					// Calculate the differences between the hours, minutes, and seconds
					new_runtime_h = (h_now - h_start);
					new_runtime_m = (m_now - m_start);
					new_runtime_s = (s_now - s_start);
					
					// Handle negative minutes
					if (new_runtime_m < 0) {
						new_runtime_h -= 1
						new_runtime_m = 60 - (new_runtime_m * -1)
					}
					
					// Handle negative seconds
					if (new_runtime_s < 0) {
						new_runtime_m -= 1
						new_runtime_s = 60 - (new_runtime_s * -1)
					}
					
					// Return a new string containing the new runtime
					runtime_str = (new_runtime_h + 'h:' + new_runtime_m + 'm:' + new_runtime_s + 's');
				}
                
                
                // Test the code
                calcRuntime()
                
                println runtime_str
                
 
            ]]></script>
        </robot>
    </robotics-flow>
    <export include-original-data="true"/>
</config>
2 Likes

:muscle: Greetings! I’m glad that I was able to help :smile:

1 Like