Checking value against the current date.

Ranorex Spy, Recorder, and Studio.
cartographer7
Posts: 5
Joined: Tue Sep 13, 2016 10:54 pm

Checking value against the current date.

Post by cartographer7 » Tue Sep 13, 2016 11:08 pm

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?

User avatar
RobinHood42
Posts: 238
Joined: Fri Jan 09, 2015 3:24 pm

Re: Checking value against the current date.

Post by RobinHood42 » Thu Sep 15, 2016 3:12 pm

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

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Checking value against the current date.

Post by krstcs » Thu Sep 15, 2016 4:07 pm

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.
Shortcuts usually aren't...

cartographer7
Posts: 5
Joined: Tue Sep 13, 2016 10:54 pm

Re: Checking value against the current date.

Post by cartographer7 » Thu Sep 15, 2016 6:54 pm

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?

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Checking value against the current date.

Post by krstcs » Thu Sep 15, 2016 7:17 pm

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.
Shortcuts usually aren't...

cartographer7
Posts: 5
Joined: Tue Sep 13, 2016 10:54 pm

Re: Checking value against the current date.

Post by cartographer7 » Thu Sep 15, 2016 9:19 pm

That's exactly what I needed. Thanks a bunch!

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Checking value against the current date.

Post by krstcs » Thu Sep 15, 2016 9:48 pm

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.
Shortcuts usually aren't...