Page 1 of 1

Convert dates from string to datetime and sort in different

Posted: Fri Nov 10, 2017 11:35 am
by faheem412
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.

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

Posted: Fri Nov 10, 2017 1:31 pm
by Vaughan.Douglas
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.

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

Posted: Mon Nov 13, 2017 10:31 am
by odklizec
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 ;)

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

Posted: Mon Nov 13, 2017 11:30 am
by faheem412
Thank you guys for the reply :D

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

Posted: Mon Nov 13, 2017 1:15 pm
by Vaughan.Douglas
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!