Link to home
Start Free TrialLog in
Avatar of jbh_blue1
jbh_blue1

asked on

string to date conversion in vb.net/asp.net vs 2010


How do I simply convert a string from a session var to a date in asp.net/vb.net 2010

Session("todate") = "01012010"

cDate(session('todate")) returns

"Conversion from string "01012010" to type 'Date' is not valid."
Avatar of nepaluz
nepaluz
Flag of United Kingdom of Great Britain and Northern Ireland image

You'll need to use the formatdatetime function and specify the format of the string, eg
Dim MyDate = FormatDateTime("01012010", "MMddyyyy")

Open in new window

here I am assuming your string is Month, Date and Year. If it is Date, Month and year you have to use
Dim MyDate = FormatDateTime("01012010", "ddMMyyyy")

Open in new window

Avatar of Gerwin Jansen
Explanation why cDate is not working: is doesn't recognize your string as a date. Try your string like this: "01 01 2010" or "01/01/2010"
Avatar of jbh_blue1
jbh_blue1

ASKER

 to nepaluz:

FormatDateTime("01012010", "MMddyyyy")
 
returns ...

{"Conversion from string "01012010" to type 'Date' is not valid."}
    _HResult: -2147467262
    _message: "Conversion from string "01012010" to type 'Date' is not valid."
    Data: {System.Collections.ListDictionaryInternal}
    HelpLink: Nothing
    HResult: -2147467262
    InnerException: Nothing
    IsTransient: False
    Message: "Conversion from string "01012010" to type 'Date' is not valid."
    Source: "Microsoft.VisualBasic"
    StackTrace: "   at Microsoft.VisualBasic.CompilerServices.Conversions.ToDate(String Value)"
    TargetSite: {System.DateTime ToDate(System.String)}


to gerwinjansen:

no modification to the input string is acceptable, the date can be entered as 01011980 or 111980 so parsing isn't really an option
>>the date can be entered as 01011980 or 111980 so parsing isn't really an option
If that's the case then finding a solution will be very difficult. I suggest that you have fixed fields in your input or at least validate.
ASKER CERTIFIED SOLUTION
Avatar of Imran Javed Zia
Imran Javed Zia
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There are many different ways to do that. Here are 2 of those, assuming that you string is in the format MMddyyyy:
Dim yourDate As String = "01012010"
Dim result As Date

result = CDate(yourDate.Substring(0, 2) & "/" & yourDate.Substring(2, 2) & "/" & yourDate.Substring(4, 4))
result = DateSerial(CInt(yourDate.Substring(4, 4)), CInt(yourDate.Substring(0, 2)), CInt(yourDate.Substring(2, 2)))

Open in new window

The second one is a little more complex, but is independant of the control panel, so it might be safer to use.

If your string is ddMMyyyy, simply invert the 0,2 and 2,2 Substrings in the expressions.
My Bad. Here's one that works
Dim MyString = "01012010"
If the month is the first two characters
Dim FirstDate = New DateTime(MyString.Substring(4), MyString.Substring(0, 2), MyString.Substring(2, 2))

Open in new window

else if the month is the 3rd and 4th characters
Dim SecondDate = New DateTime(MyString.Substring(4), MyString.Substring(2, 2), MyString.Substring(0, 2))

Open in new window

thanks
I don't understand why you accept a solution that parses 01012010 because you state: "the date can be entered as 01011980 or 111980 so parsing isn't really an option"

Does the solution you accepted work for 111980 as input?