Page 1 of 1

Checking value against the current date.

Posted: Tue Sep 13, 2016 11:08 pm
by cartographer7
I'm new to Ranorex, and I'm trying to check a string value (mm/dd/yyyy format) against the current date. What's the best way to go about doing this?

Re: Checking value against the current date.

Posted: Thu Sep 15, 2016 3:12 pm
by RobinHood42
Hi cartoprahper7,

The question isn't direclty related to Ranorex, but rather to C# in general. I would recommend reading the following stackoverflow post:

http://stackoverflow.com/questions/3059 ... etime-in-c

Cheers,
Robin

Re: Checking value against the current date.

Posted: Thu Sep 15, 2016 4:07 pm
by krstcs
There are two ways, both using the DateTime struct's Today property ("DateTime.Today"):

1. Convert the string value to a DateTime type object. Then use the object's Equals() method from one to the other.

Code: Select all

string myDate = "09/15/2016";
if (DateTime.Parse(myDate).Equals(DateTime.Today)) {
  //do your stuff here
}
2. Convert today's date to a string and compare them using the == comparison (or you can use Equals() again).

Code: Select all

string myDate = "09/15/2016";
if (DateTime.Today.ToString("MM/dd/yyyy") == myDate) {
  //do your stuff here
}
The best one is probably number 1, because it should be more consistent and you don't have to worry about the date format, .NET will handle that for you.

Re: Checking value against the current date.

Posted: Thu Sep 15, 2016 6:54 pm
by cartographer7
Ah, I guess I wasn't clear enough. I'm wanting to use a verify action to check the data in a field. The code you showed me could be useful, but I have no idea how to use it with a verify action. So far I've only figured out how to verify against a fixed value or data connection to Excel. How do I set up a verify action which will use the comparison code you just gave me?

Re: Checking value against the current date.

Posted: Thu Sep 15, 2016 7:17 pm
by krstcs
Ah, ok!

That will still have to be done in user-code land though...

1. Create the Validate Action in the action table in the recording module. Don't worry about which one, it's just a template.
2. Right-click and select "Convert to usercode".
3. Double-click the new usercode action -> This will open the usercode module linked to this recording.
4. In the code it will look like this:

Code: Select all

Report.Log(...);
Validate.<whatever type of validation you chose>(...);
Change the validation line to this:

Code: Select all

Validate.AreEqual(DateTime.Today, DateTime.Parse(myDate));
Now you can change the Report.Log line to reflect the new validation you are doing.

Re: Checking value against the current date.

Posted: Thu Sep 15, 2016 9:19 pm
by cartographer7
That's exactly what I needed. Thanks a bunch!

Re: Checking value against the current date.

Posted: Thu Sep 15, 2016 9:48 pm
by krstcs
You're welcome!

I've been using Ranorex for a while, deep in the C#, and I still do things like this where I use something that Ranorex provides out-of-the-box as a template for what I really want in code, changing it to do exactly what I need. Handy trick to keep in the toolbox for many occasions where you know what you WANT to do, but not exactly how to get Ranorex to do it.