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."
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"
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.ListDi ctionaryIn ternal}
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.Comp ilerServic es.Convers ions.ToDat e(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
FormatDateTime("01012010",
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.ListDi
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.Comp
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
There are many different ways to do that. Here are 2 of those, assuming that you string is in the format MMddyyyy:
If your string is ddMMyyyy, simply invert the 0,2 and 2,2 Substrings in the expressions.
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)))
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 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))
else if the month is the 3rd and 4th charactersDim SecondDate = New DateTime(MyString.Substring(4), MyString.Substring(2, 2), MyString.Substring(0, 2))
ASKER
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?
Does the solution you accepted work for 111980 as input?
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 useOpen in new window