Convert string to Date and Compare

Hi Experts ,

I have Date as strInputDate : “23.08.2019” - String.

I want to compare it with SystemDate.

Approach i used :

  1. Replace . with /
    _strInputDate.replace(“.”,“/”) - I got : 23/08/2019

  2. Add Hours : just to make it valid datetime
    ValidThroDateAddHours = _strInputDate + “” 11:06:30 AM"" – I got “23/08/2019 11:06:30 AM”

  3. Next
    DateTime.ParseExact(ValidThroDateAddHours,“dd/MM/yyyy hh:mm:ss tt”,Nothing).ToString(“dd/MM/yyyy HH:mm:ss”)

Getting Error at Step 3 : Assign: String was not recognized as a valid DateTime.

Please Suggest what Am i doing wrong.

Also , if there any better alternative. all i want is to compare

string - “23.08.2019” with CurrentDate

Thanks

1 Like

try like this buddy @mukeshkala

StrOutputDate = Datetime.ParseExact(strInputDate,“dd.MM.yyyy”,System.Globalization.CultureInfo.InvariantCulture).ToString(“dd/MM/yyyy”)

Then to compare this date with current date we can do like this
but not with string datatype variable rather with datetime variable like this
DtOutputDate = Datetime.ParseExact(strInputDate,“dd.MM.yyyy”,System.Globalization.CultureInfo.InvariantCulture)

Then you can compare like this to get the value you
DtOutputDate - Now
or
DtOutputDate > Now
or
DtOutputDate < Now

Kindly try this and let know for any clarification or queries
Cheers @mukeshkala

3 Likes

did that work buddy @mukeshkala

DateTime dt1=DateTime.ParseExact(strInputDate ,"MM-dd-yyyy",null);
DateTime dt2=Date.Today.ToShortDateString
int cmp=dt1.CompareTo(dt2);

   if(cmp>0) {
       // date1 is greater means date1 is comes after date2
   } else if(cmp<0) {
       // date2 is greater means date1 is comes after date1
   } else {
       // date1 is same as date2
   }

Try like this

1 Like

@Palaniyappan

Awesome Bro :slight_smile: That Worked Perfectly !!

Kudos to you !!

1 Like

Fantastic @mukeshkala
Kindly close this topic with right comment marked as solution that could help others as well looking for ideas under your topic
Cheers @mukeshkala

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.