Subtract one time period with the other obtained from Excel


In a excel table;
There are two rows-
login and logout.

I need the total time spent(i.e. logout - login )
and save it in the next new column(totalTime).
I’m able to add the new column;
not able to subtract it.
can anyone help?

Thanks in advance!

Hi @ray_sha,

Try below code
Assign the values into two variable called ‘time1’ and ‘time2’ datatype as the string

time1 =  "7:00"
time2 = "16:00"

Create another variable called ‘duration’ datatype as TimeSpan
Assign it as below

duration = DateTime.Parse(time2).Subtract(DateTime.Parse(time1))

  1. Write cell B4 as with Formula B3-B2 .
  2. Auto fill range activity to fill rest of the rows.

Source: Assign
Message: String was not recognized as a valid DateTime.

This is the error

@ray_sha, Ensure datatype of duration variable is string and it should work

Hi @ray_sha

Check the attachment

TimeDiff.xaml (4.9 KB)

Hope this helps


TimeDiffCalc.xaml (5.4 KB)

Please check the attached Xaml.

Hope it helps you

Both the values should be string initially and then it will give the differenct between two values as Datetime

I need it from the excel sheet; as given in the above query

Source: Assign
Message: String was not recognized as a valid DateTime.
Then this will be the error


Source: Assign
variableName = DateTime.Parse(“16:00”).Subtract(DateTime.Parse(“7:00”)).ToString

Where datatype of variableName is String. (Replace string value with actual variable e.g. time1, time2)
I tried this and it is working for me.


Hey I’m fetching the value from the excel file

It must accept value from the excel file(ss of which is shown above)
and store the value in the new column (beside logout) as totaltime

Why cant you frame a values as below and check .
login :- 03/06/2020 07:00:00
logout :- 03/06/2020 16:00:00

I have used todays date ( Datetime.Now)


If we give values directly then it works;
But I want to take the value from the excel file and thus its giving error

can you please elaborate on that one?

I didn’t understand you, can you please elobarate?

@ray_sha Can you Follow these Steps :

  1. Use Workbook Read Range Activity and read the Excel File, Get Output as Datatable.
  2. Use For Each Row Activity for the Output Datatable
    Inside For Each Row, Use a Message Box with value as row(“login”).ToString

Send the Screenshot of what Displays in the Message Box , Based on that value we can Apply what method is Necessary

1 Like

@ray_sha, Surprisingly it is working for me. Please check attached workflow and excel.
Attached workflow is for demonstration only you have to iterate through all rows as needed.

Main.xaml (5.2 KB) Test.xlsx (9.0 KB)

1 Like

This is the output


@ray_sha Ok, That is Confusing :sweat_smile: , Can you Check what is the Data Type of logout and login in Excel File? Is it Date or Custom or Something Else ?

Hi Use the below and check pls

DateTime.ParseExact(“06/03/2020 18:00:00”, “MM/dd/yyyy HH:mm:ss”, System.Globalization.CultureInfo.InvariantCulture)-DateTime.ParseExact(“06/03/2020 08:30:00”, “MM/dd/yyyy HH:mm:ss”, System.Globalization.CultureInfo.InvariantCulture)).TotalHours.ToString()

1 Like