Trying to get date time from Excel in UIPATH

Hi,

I have a column in Excel, called “StartTime” with MM/dd/yyyy hh:mm:ss format. I want to get that “StartTime” data in Uipath and subtract that with the “CurrentTime” ie date time.now (RunningTime= CurrentTime- StartTime).
I tried getting the “Starttime” in a string using (StrTime = row.item(“StartTime”).toString and the next step to convert it to a Datetime using StartTime= convert.datetime(date time.ParseExact(Strtime,“MM/dd/yyyy hh:mm:ss”,system.Globilization.CultureInfo.InvariantCulture))
Is this the correct logic because I keep getting Error: "Assigned String was not converted to date time.

MM/dd/yyyy HH:mm:ss can be tried as maybe as hours are also 13, 14

convert.datetime(date time.ParseExact(Strtime,“MM/dd/yyyy hh:mm:ss”,system.Globilization.CultureInfo.InvariantCulture))

here we do have a double parsing tring into datetime into datetime

datetime.ParseExact(Strtime,"MM/dd/yyyy HH:mm:ss",system.Globilization.CultureInfo.InvariantCulture) should work

also important is the value from Strtime, maybe trimming is helping:
StrTime = row.item(“StartTime”).toString.Trim()

1 Like

Hi @Nishanthi_Sankar

Try to use this Expression

  • Assign first variable as (Default_Time) = Datetime.now.tostring
  • Assign secondvariable as (Final_Time) = Datetime.now.tostring

Convert.ToString(
(DateTime.ParseExact( Default_Time, “HH:mm:ss:ff”,System.Globalization.CultureInfo.InvariantCulture))-
(DateTime.ParseExact(Final_Time, “HH:mm:ss:ff”,System.Globalization.CultureInfo.InvariantCulture)))

1 Like

Thanks Peter.

I tried with StartTime=datetime.ParseExact(Strtime,“MM/dd/yyyy HH:mm:ss”,system.Globilization.CultureInfo.InvariantCulture) and also used Trim() in StrTime and getting the same Error: “String was recognized as valid date time”

let us know which value was used for strtime

ensure that no blank / uinvalid values are used. In such cases a quick analysis statement entered into the immedia panel can help

Hi Peter, Thank you! I am succesfuly able to proceed to subtraction after following your details.
For subtracting,I used Running time= (CurrentTime.subtract(StartTime)).toString(“hh:mm”) for data(Current time : 07/28/2021 18:00 AND StartTime= 07/26/2021 13:00), and it gives incorrect result as 23:52
The result should be nearly 48 Hrs time.

Hi @Nishanthi_Sankar - Is this your expected result?

if your country format/system format is dd/MM/yyyy then datetime.parse will not work. so in that case, just replace it with datetime.parseexact and use total hours, as shown below…

Hi Prasath, Yes I need difference of total Hours as result.

I have 3 Variables( CurrentTime of type: Datetime, StartTime of type: DateTime, and RunningTime of type string). To calculate the RunningTime: (RunningTime= CurrentTime- StartTime), I tried to use your statement and getting error that "Conversion from “double to string " is not allowed”

after the totalhours, just use .tostring…

Got it. Please let me know, how you are getting the current time (like “now”) and starting time? Provide sample values…PLease check below…Here StartDate and End Date are both datetime variables…

1 Like

That worked Wonderfully! Thank you so much. I am able to get total running hours (Eg: 46Hrs).
If I want to check that Total hours > 6 hours, Can I assign 06:00 hours to a string variable and compare? I just wanted to make sure it won’t take 6 o clock instead of 6 hrs?

it will not work. because your total hours is string…which you have to convert it to int OR use totalhours as Int variable(with.tostring) and then your 6 hours should again needs to converted to int and then use it in the IF Condition…

Thank you,Like you said, Can I try this in If condition: Cint(RunningTime)> Cint(ConstantTime)?
where Running Time is the Total running hours of Type String , ConstantTime is the 06:00 of type string

I tried the same and I am getting error that “Conversion from String to type Int is not Valid”

Just stepped out…can you please show me how you have tried ?

Sure,Thank you so much.
I will attach the screenshots, (Runningtime(String)-> TotalHours, ConstantTime(String)->1 Hour)

@Nishanthi_Sankar - cint(“1 hour”) will fail because it cannot covert the text. So you have to use

Cint(“1 hour”.split(" "c)(0))…this will work…please try and let me know…

image

1 Like

I am able to Understand now, I am implementing it but for some reason, it is running slowly, I will let you know it sooner. Thank you so much for your spontenious response!

1 Like

I tried by setting all 3 variables as String and tried your statement to subtract and that worked as well. Thank you so much. But since I need totalHours calculation, I used " (CurrentTime.subtract(StartTime).totalhours).toString.
Thank you so much!

1 Like

I was able to get the desired result, Thank you very much.

@prasath17 I wanted to compare the dates by getting the date (MM/dd/yyyy HH:mm:ss) from an excel file and comparing it with today’s date.

I am getting it by using : StrTime = row.item(“StartTime”).toString.trim() and then for converting to Datetime, I used( StartTime=datetime.ParseExact(Strtime,“MM/dd/yyyy HH:mm:ss”,system.Globilization.CultureInfo.InvariantCulture) .
For comparing , did : datetime.ParseExact(Strtime,“MM/dd/yyyy HH:mm:ss”,system.Globilization.DateTimeFormatInfo.InvariantInfo)= Today
I end up getting error: String was not recognised as valid date time error.