Convert string variable which stores date into Date time format

In_strInvoiceDate is the string variable which holds the date in dd/MM/yyyy format…I want to use this variable as a argument and process the date by adding 30 days into it according to holidays…so firstly how can I convert this string variable into Datetime

@rutuja.y

Use CDate to convert String to DateTime variable.

        CDate("varStringDate")
1 Like

a dd/MM/yyyy format we do parse with

DateTime.ParseExact(In_strInvoiceDate,“dd/MM/yyyy”, CultureInfo.InvariantCulture)

ensure following:
grafik

We are using this conversion as CDate is expecting MM/dd/yyyy format:
grafik
Line1 - wrong conversion as day Month is swapped
Line2 - its failling as there is no Month with the number of 17

@rutuja.y

Check below for your reference

Reference

Hope this may help you

Thanks

What if I want to add 30 days into in_strInvoiceDate then what will be expression?

I tried by using EndDate=DateTime.parseExact(in_strInvoiceDate.AddDays(30),“dd/MM/yyyy”,culture info. Invariant Culture)…but it not works

please tell us allways what is not working, thanks

  • System.Globalization was added to the imports?
  • culture info. Invariant Culture is a copy and paste issue?

System.Globalization added…and no copy paste issue…only want to chech whether my expression is correct or not after adding 30 days into that.
EndDate=DateTime.ParseExact(in_strInvoiceDate.AddDays(30),“dd/MM/yyyy”, Cultureinfo.InvariantCulture)

we didn’t shared this code line with you.

Based on your input:

we shared with you a conversion option to parse a string into a datetime.

AddDays is to use on a variable of datatype datetime and cannot be used on a string variable.

  • first step: convert to dateteime
  • second step: add days like EndDate.AddDays(30)

Hi

Hope the below expression would help you resolve this

datetime_input = Datetime.ParseExact(in_strInvoiceDate.ToString,”dd/MM/yyyy”, system.Globalization.CultureInfo.InvariantCulture).AddDays(30)

Where datetime_input is a variable of type System.DateTime

Cheers @rutuja.y

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