How to get Date from Date Time

Best practices, code snippets for common functionality, examples, and guidelines.
thirukumar
Posts: 8
Joined: Tue Jun 07, 2016 8:30 am

How to get Date from Date Time

Post by thirukumar » Wed Jul 06, 2016 12:35 pm

Hi,

In my code i am using the following line of code
Today date: 06-07-2016
string timestamp = System.DateTime.Today.ToString("d"); (or)
string timestamp = System.DateTime.Now.ToString("d");

for above both code i am getting output as "06-07-2016" instead of output as "6".

d - day "6"
dd - day "06"
ddd - Day of week "Wed"
dddd - Day of Week "Wednesday"

Please Help me how can i get output only "6"?

Thanks,
Thirukumar.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3937
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: How to get Date from Date Time

Post by odklizec » Wed Jul 06, 2016 12:52 pm

Hi

Try to use "%d" parameter in ToString method (instead of just "d")...

Code: Select all

string timestamp = System.DateTime.Today.ToString("%d");
Hope this helps? ;)
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

thirukumar
Posts: 8
Joined: Tue Jun 07, 2016 8:30 am

Re: How to get Date from Date Time

Post by thirukumar » Wed Jul 06, 2016 12:59 pm

Hi odklizec,

Thank You it worked. But Can you tell me the reason why we need to use "%d" instead if "d"?


Thanks,
Thirukumar.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3937
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: How to get Date from Date Time

Post by odklizec » Wed Jul 06, 2016 1:12 pm

Hi,

I think the answer you are looking for is here:
https://msdn.microsoft.com/en-us/librar ... Specifiers
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration