How to return only today's Date (without time)

Class library usage, coding and language questions.
houseofcutler
Posts: 52
Joined: Fri Mar 21, 2014 4:22 pm

How to return only today's Date (without time)

Post by houseofcutler » Wed May 07, 2014 3:55 pm

Hi All,

I have been searching for a solution to this for a while now. I can get the current date and time using with of these commands
string varDateTime = System.DateTime.Now.ToString();
string varDate = System.DateTime.Now.Date.ToString();
Both return a datetime format such as - 07/05/2014 00:00:00 - This is no surprise given that that is what I used to get the data.

I have been able to find a way to strip this back to just the date - I need this value to be able to use it in some validation tests.

I have found many suggested C# solutions to what I am trying to do but non of them work within the contexts of Ranorex user code.

Does anyone out there have a solution please?

Many thanks

Ben

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

Re: How to return only today's Date (without time)

Post by krstcs » Wed May 07, 2014 4:09 pm

All C# solutions should work in Ranorex User Code, assuming the libraries are imported correctly (DateTime is in System, so that shouldn't be an issue here). What specifically isn't working?

As far as the format, all DateTime properties return the date AND time. The "Date" property returns the time as midnight. You will need to use the ToString(string format) method, passing in the date format (as a string) that you want. See the .NET documentation for imformation on the format: http://msdn.microsoft.com/en-us/library ... 10%29.aspx.

You can do something like this:

Code: Select all

string date = System.DateTime.Today.ToString("ddmmyy");
This should return just the DATE portion of today's date. (You can use "Today" or "Now", it should work either way.)
Shortcuts usually aren't...

houseofcutler
Posts: 52
Joined: Fri Mar 21, 2014 4:22 pm

Re: How to return only today's Date (without time)

Post by houseofcutler » Thu May 08, 2014 11:37 am

Thanks Krstcs - I had tried something similar before which didnt work - obviously I did it wrong. Yours works fine.

To get the format I wanted I did the following:
string date = System.DateTime.Today.ToString("dd/MM/yy");
Now I can continue building my test... :D

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

Re: How to return only today's Date (without time)

Post by krstcs » Thu May 08, 2014 3:40 pm

You are welcome!
Shortcuts usually aren't...