Convert a string to date

Hi,

How can convert “12/05/2018” which is a string to date type ?

3 Likes

Hi, you probably have some answer on the forum already

Already checked all the answer on the forum before i post, none worked for me, each time i have a kind of error…

@mz3bel

Try this:

DateTime VarDate = Cdate(“12/05/2018”)

1 Like

Conversion from string “myvar” to type ‘Date’ is not valid.

1 Like

fine is 12/05/2018 is of format dd/MM/yyyy or MM/dd/yyyy
if its dd/MM/yyyy
then out_date = Datetime.ParseExact(“12/05/2019”,“dd/MM/yyyy”,System.Globalization.CultureInfo.InvariantCulture)
where out_date is a variable of type datetime

or if its of format “MM/dd/yyyy”
then
out_date = Datetime.ParseExact(“12/05/2019”,“MM/dd/yyyy”,System.Globalization.CultureInfo.InvariantCulture)

Cheers @mz3bel

10 Likes

@mz3bel

Try this:

edate = “12/05/2018”

DateTime varDate = Date.ParseExact(edate, “dd/MM/yyyy”, System.Globalization.DateTimeFormatInfo.InvariantInfo)

2 Likes

String was not recognized as a valid DateTime

1 Like

i mentioned two formats buddy
did the either one worked
Cheers @mz3bel

Yes, i used the one i have, found the error, when i copied the variable i took the " " with it. didn’t see it

1 Like

@Palaniyappan

is it normal that i shows 04/21/2019 instead of 21/04/2019, even though my string was 11/07/2019 ?

when we try to use parseexact in datetime, we need to clear with the format that we specify buddy…whatever may be the format of input, but the format that we specifiy next to the input must match with its format (the highlighted one)
out_date = Datetime.ParseExact(“12/05/2019”,“MM/dd/yyyy”,System.Globalization.CultureInfo.InvariantCulture)

@mz3bel

i’m a bit lost here,

my string is 22/072019 so it’s dd/MM/yyyy right?

so out_date = Datetime.ParseExact(“ 22/072019”, “dd/MM/yyyy” ,System.Globalization.CultureInfo.InvariantCulture) right ?

because i tried, to change from dd/MM/yyyy to MM/dd/yyyy

i get this error : The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.

1 Like

this is right…good
but when we try to convert a date from 22/07/2019 where
22 is date
07 is month
2019 is year
-----to a date format of MM/dd/yyyy, 22 becomes month, 07 becomes date and 2019 year

we dont have 22 as month buddy thats why showing error as
The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.

Cheers @mz3bel

Yes, i know.

So how can i change it to dd/MM/yyyy please ? and also keep only date without time

i tried to add this at the end didn’t work, even some does for them ?! .ToString(“MM/dd/yyyy”)

Never mind, i found the solution.

I used this on assing out_date.ToString(“dd/MM/yyyy”) as out_date is date type.

Thank you all for you help

1 Like

Fantastic
Cheers @mz3bel

1 Like

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