How to get the time along with AM/PM

iam reading the Excel to get the date and time value in a string format

and iwant only time value along with AM or PM. so iam using
datetime.ParseExact(read_time,“dd/MM/yyyy h:mm:ss tt”,system.Globalization.CultureInfo.InvariantCulture).ToShortTimeString but still iam not getting

where read_time is string variable

Please help me ASAP

Note: In Excel I have mentioned AM which is not visible in screenshot

Main.xaml (12.5 KB)
project.json (655 Bytes)

Hi @dhanushm,

Use below format

"dd/MM/yyyy h:mm:ss a"


Hi @dhanushm,

Please refer the below expression

If you have a string like this in excel: “05/18/2019 8.30” and if you want only time value along AM and PM then use the below expression

Datetime.ParseExact(“05/18/2019 8.30”,“MM/dd/yyyy”,system.Globalization.CultureInfo.InvariantCulture).ToString(“hh:mm:ss tt”)

No still IAM not getting it is throwing error saying invalid datetime format

Hi @dhanushm,

Can you use the above expression in write line and let me know if you are getting any error.

Provide your Excel file.

Try the below one

Datetime.ParseExact(“5/18/2019 8.30”,“M/dd/yyyy”,system.Globalization.CultureInfo.InvariantCulture).ToString(“hh:mm:ss tt”)

In your scenario

Datetime.ParseExact(row(“Date/Time”).ToString,“M/dd/yyyy”,system.Globalization.CultureInfo.InvariantCulture).ToString(“hh:mm:ss tt”)

Hi @dhanushm,

Use below code

Convert.ToDateTime(readtime).ToString("hh:mm:ss tt")

I want only time I don’t want date and my Excel Book1.xlsx (9.6 KB)

iam getting only time without AM/PM

Hi @dhanushm,

Have you used this expression

Datetime.ParseExact(row(“Date/Time”).ToString,“M/dd/yyyy”,system.Globalization.CultureInfo.InvariantCulture).ToString(“hh:mm:ss tt”)

And the error string was not recognized as valid date time because you are inside for each and for the 3rd row you have empty value under date column that’s y your expression is failing

no iam Not getting String was not recognized as a valid DateTime.

Does this resolve your issue?

no bro still iam getting error

it works well for me. is this what you wanted?

i also just changed the expression of the 2nd message box to

datetime.ParseExact(read_time,“dd/MM/yyyy h:mm:ss tt”,system.Globalization.CultureInfo.InvariantCulture).ToString

Hi @dhanushm

in Read Range activity can please check “PreserveFormat” and try it.

Vijay Kumar C.

1 Like

i think i need to reinstall my uipath studio … it didnt give me any error on the first 2 attempts but now


Source: Message Box

Message: String was not recognized as a valid DateTime.

Exception Type: System.FormatException

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.FormatException: String was not recognized as a valid DateTime.
at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)
at lambda_method(Closure , ActivityContext )
at Microsoft.VisualBasic.Activities.VisualBasicValue1.Execute(CodeActivityContext context) at System.Activities.CodeActivity1.InternalExecuteInResolutionContext(CodeActivityContext context)
at System.Activities.Runtime.ActivityExecutor.ExecuteInResolutionContext[T](ActivityInstance parentInstance, Activity1 expressionActivity) at System.Activities.InArgument1.TryPopulateValue(LocationEnvironment targetEnvironment, ActivityInstance activityInstance, ActivityExecutor executor)
at System.Activities.RuntimeArgument.TryPopulateValue(LocationEnvironment targetEnvironment, ActivityInstance targetActivityInstance, ActivityExecutor executor, Object argumentValueOverride, Location resultLocation, Boolean skipFastPath)
at System.Activities.ActivityInstance.InternalTryPopulateArgumentValueOrScheduleExpression(RuntimeArgument argument, Int32 nextArgumentIndex, ActivityExecutor executor, IDictionary2 argumentValueOverrides, Location resultLocation, Boolean isDynamicUpdate) at System.Activities.ActivityInstance.ResolveArguments(ActivityExecutor executor, IDictionary2 argumentValueOverrides, Location resultLocation, Int32 startIndex)
at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

lets revisit the issue here.

  1. we have a excel file with a bunch of information
  2. under column D we have date and time mentioned
  3. we need a message box to pop up with only hh:mm:ss AM/PM for every row

is this correct?

yes bro

1 Like

why are we complicating life so much?

try this.

Main (8).xaml (12.2 KB)

Hi @dhanushm,

There are so many solutions above which was created for your problem and each solution by everyone works, just learn from that and see where you are getting the error and change the expression accordingly.

If you are looking for the exact solution for your requirement you can Get the solution but can never understand the expression and based on errors only you have to learn.

Can you check the expression once again and see where is the error and learn the expression

@anil5 the expression shows that it is correct but i am not sure why i keep getting a error on it.

i will like to learn more on that and i hope it would be ok if you can share some thoughts on the same please :slight_smile:

1 Like

Hi @VirajN,

Suppose we have this string “5/18/2019 8.30” and i want only time from this suppose 8.30 AM.

Datetime.ParseExact(“5/18/2019 8.30”,“M/dd/yyyy”,system.Globalization.CultureInfo.InvariantCulture).ToString(“hh:mm:ss tt”)

So if you want to convert a string which is representing a date and time to a different format you have to use Datetime.parseexact

“5/18/2019 8.30” - Any string with date and time format
“M/dd/yyyy” - We have to specify the format of this string “5/18/2019 8.30”
system.Globalization.CultureInfo.InvariantCulture - a parameter
ToString(“hh:mm:ss tt”) - Here i am specifying which format i want from the above string.

The output of above variable is a string variable.