How to check if given time string variable is within the time range?

Hi,

I am attempting to check if a given time string variable type is within a time range.

Examples of a time string variable could be anything like eg. “27/08/2021 4:30:00 PM”, “31/08/2021 11:00:00 AM”

What is the best approach to be able to identify if the time from the string variable is within the time range for example: 10am - 2pm

So I want to be able to return the true boolean value if the example “31/08/2021 11:00:00 AM” is within the time range 10am - 2pm

The given time is not in date time type as it’s set in a string variable type

Hi @ciaramkm

Store the string into a variable, say - inputDateTimeStr

image

Convert the string into Date Time Format

image

inputDateTime = DateTime.ParseExact(inputDateTimeStr, "dd/MM/yyyy h:mm:ss tt", Globalization.CultureInfo.InvariantCulture)

Note: The variable type of inputDateTime variable is System.DateTime

Use the if condition Acitivity as follows

inputDateTime.TimeOfDay <= New TimeSpan(14,0,0) And inputDateTime.TimeOfDay >=New TimeSpan(10,0,0)

For your reference

Time in a Range.xaml (5.4 KB)

Hi @ciaramkm

Here is the sample workflow
Time range.xaml (5.0 KB)

Hi @Gokul001,

Could you also mention in the post what your pseudo code is? How the output looks like? It will improve the quality of your answers.

A sample.xaml file is good but without a description in a reply it helps very little to people reading the thread.

@kumar.varun2 already has a good answer, is your xaml file is showing an alternative method or is it similar to the solution already posted?

Only if the approach / output is visible will one reading the thread have the ability to choose which solution they want to try.

I hope you take this as a constructive feedback.

Hi @jeevith
I will make sure to provide some description with a sample.XAML file

Hi, thank you for the suggestion. If a different input example was given without any date and just the time say for example an input string will be “17:00 PM”. Is it possible to be able to still use this time alone without any date and in this format and if its within the time range as shown above?

I have tried using DateTime.ParseExact(TestString, “hh:mm”, Globalization.CultureInfo.InvariantCulture)

From the input “17:30 PM” - I dropped the PM so used the above command without the tt inside the input string because it is already in a 24hour format. I kept receiving the error “Assign: String was not recognized as a valid DateTime.”

Is there a correct command I could be able to use using the 24 hour format for this original question?

Please check this…In this case, Datetime.parse will help you…

image

Please note , since I am in US , if I give dd/MM/yyyy format datetime.parse will thrown an error. So but if your country format/system time is dd/MM/yyyy you can use it as is.


image

Hope this helps…

Hi.

Thank you - there will be no dates included as is just the time. That suggestion worked but unable to use it for the last part in the if statement where it then checks if the time is within the time range or not because it is in a string

I am confused now. what is your exact input values? Initially you said, it is “27/08/2021 4:30:00 PM”, “31/08/2021 11:00:00 AM” and then you said, "“17:00 PM”… So please clarify??

Yes that is right but then with this time input I need to check if it is within a time period -

For example an input is “17:00 PM” and I need to check if this time is within say 10:00 AM and 2:00 PM

So it would be put into an if else activity statement where it checks if 17:00 PM is within the time period or outside the time period. In one of the suggestions above it has a command in the if condition but because the input value is in a string type not datetime type so unable to use this input inside the if condition if that makes sense?

Got it…So input string will be always time with 24 hrs format right? etc 17:00 PM , 10:00 AM

That is correct yes

@ciaramkm - Here you go…

  1. Time is 11 am - Case is “True” → between 10 am to 2 pm

  1. Time is 5 pm - Case is “False” → between 10 am to 2 pm

 datetime.parse("17:00 PM").TimeOfDay <=  new TimeSpan(14,0,0) and datetime.parse("17:00 PM").TimeOfDay >= new TimeSpan(10,0,0)

You can datetime.parse(“Your String”).TimeOfDay → Save this to Timespan variable…as shown below…

Hope this helps…

1 Like

That’s very helpful, thank you :slight_smile:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.