• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1148
  • Last Modified:

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."
0
jbh_blue1
Asked:
jbh_blue1
  • 3
  • 2
  • 2
  • +2
1 Solution
 
nepaluzCommented:
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

0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
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"
0
 
jbh_blue1Author Commented:
 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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Gerwin Jansen, EE MVETopic Advisor Commented:
>>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.
0
 
Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
Hi,
Please try following:

Dim dt As DateTime = DateTime.ParseExact("01012010", "MMddyyyy", System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat)

Thanks
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
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.
0
 
nepaluzCommented:
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

0
 
jbh_blue1Author Commented:
thanks
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
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?
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now