Change date format from string to date

Hello

Is there any way to change a string I am pulling from a data table which is a date into a date format variable?

The string variable is below

image

Which writes as a sting like below

image

I then want to convert this to a date variable like below

image

This is the error I am getting

image

1 Like

@Bob2 Do you want only Date , or Date with Time as well?

1 Like

Hi @Bob2

You can check this out !

string.Format("{0}", System.DateTime.Now.ToString(“yyyy-MM-dd_hh-mm-ss”))

cheers :smiley :smiley:
Happy learning :smiley:

2 Likes

Just the date

I am still getting the error after the change

image

error

I think its because I need the date format to be 16/01/2020

if(DateTime.TryParseExact(yourString,“MM/dd/yyyy hh:mm:ss”, Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, Nothing),DateTime.ParseExact(yourString,“MM/dd/yyyy HH:mm:ss”,System.Globalization.CultureInfo.InvariantCulture).toString(“dd/MM/yyyy”),Nothing)

This Code Checks if it is in a Required Date Format and then if it is true,Converts it into the Format Needed

3 Likes

So put this into the ‘startDate’ variable like below but I am still getting an error

error

image

@Bob2 Can you remove the .ToString(“dd/MM/yyyy”) and Check

try using this
variable must be of DateTime type
DateTime.ParseExact(RecievedDate,“dd/MM/yyyy”, nothing)

2 Likes

Thanks - that worked!

1 Like

good to hear that.
Happy Programming. :slightly_smiling_face:

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