Convert dates from string to datetime and sort in different

Experiences, small talk, and other automation gossip.
faheem412
Posts: 13
Joined: Tue Oct 10, 2017 8:52 am

Convert dates from string to datetime and sort in different

Post by faheem412 » Fri Nov 10, 2017 11:35 am

Hi Team,

I need a help from you guys, got stucked to this issue for long time.
I have stored 5 dates("5 Feb"; "6 Apr"; etc..) and want convert to DateTime and validates dates are in ascending and descending order.

Kindly help me.

Vaughan.Douglas
Posts: 250
Joined: Tue Mar 24, 2015 5:05 pm
Location: Des Moines, Iowa, USA

Re: Convert dates from string to datetime and sort in different

Post by Vaughan.Douglas » Fri Nov 10, 2017 1:31 pm

Date.TryParse will get you a date object from a string object.

Code: Select all

    Public Shared Function StringToDate(ByVal dateAsString As String) As Date
        Dim myDate As Date
        Date.TryParse(dateAsString, myDate)
        Return myDate
    End Function
The next bit can be more tricky depending on what you want to do. If you want to just compare two dates you can use the compare method of the date object. However if you want to sort a list of dates, you'll probably need some fancy code.
Doug Vaughan

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

Re: Convert dates from string to datetime and sort in different

Post by odklizec » Mon Nov 13, 2017 10:31 am

Hi,

As for validating datetime order, I would use the code described here:
https://stackoverflow.com/questions/180 ... st-by-time

I would create a copy of the original list of dates, sort the duplicated list using the above code and then simply compare both lists if they are the same. If not, the sorting of original list of dates is most probably incorrect ;)
Last edited by odklizec on Mon Nov 13, 2017 12:02 pm, edited 1 time in total.
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

faheem412
Posts: 13
Joined: Tue Oct 10, 2017 8:52 am

Re: Convert dates from string to datetime and sort in different

Post by faheem412 » Mon Nov 13, 2017 11:30 am

Thank you guys for the reply :D

Vaughan.Douglas
Posts: 250
Joined: Tue Mar 24, 2015 5:05 pm
Location: Des Moines, Iowa, USA

Re: Convert dates from string to datetime and sort in different

Post by Vaughan.Douglas » Mon Nov 13, 2017 1:15 pm

odklizec wrote:Hi,

As for validating datetime order, I would use the code described here:
https://stackoverflow.com/questions/180 ... st-by-time

I would create a copy of the original list of dates, sort the duplicated list using the above code and then simply compare both lists if they are the same. If not, the sorting of original list of dates is most probably incorrect ;)
I should've figured there was a sorting solution in Linq.
+1 for you!
Doug Vaughan