DateTime.Parse method error

Hi all

I’m using in assign actitivty

DateTime.Parse(dateCurrentPerson.month+"/"+dateCurrentPerson.Day+"/"+DateTime.Today.Year)

to convert into DateTime format.

But its throwing me error like this in the attached picture.

Can any one help me .

HI @saritha

Try like this
Datetime.ParseExact(“23/02/2022”,“dd/MM/yyyy”,System.Globalization.CultureInfo.InvariantCulture).ToString(“dd/MM/yyyy”)

Regards
Gokul

Hi @saritha,

You can try with the following format -

Datetime.ParseExact(YourDateToBeParsed,YourDateFormat,System.Globalization.CultureInfo.InvariantCulture).ToString(TheFormatInWhichYouWantTheDate)

Thanks

Hi @saritha ,

You have missed to convert the values to String.

Check the below Expression :

DateTime.Parse(dateCurrentPerson.month.ToString+"/"+dateCurrentPerson.Day.ToString+"/"+DateTime.Today.Year.ToString)
2 Likes

we do understand that you want create a new datetime form persons’ day, month and current year
it looks also that dateCurrentPerson is already a datetime variable

so we can create the datetime directly by

new DateTime(now.Year, dateCurrentPerson.Month, dateCurrentPerson.Day)

Hi,

Thanks Everyone for the replies.

now its working. Thanks for the reply.

Just wanted to clarify that , will “DateTime.Parse” takes String Parameters ?

Thanks.

Hi,

Thanks for ur reply.
May I know why new DateTime is used here. I didn’t get it .
Can u explain if possible.
Thanks.

we can avoid x to y and y to x conversions when we do create directly the Datetime by using present informations. So we do not produces strings, which we are parsing again in a datetime as
new DateTime(now.Year, dateCurrentPerson.Month, dateCurrentPerson.Day) is doing this more compact and directly.

1 Like

ok got it now.

thanks.

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