tags:

views:

390

answers:

3

Hi guys,

I have a Session variable in which i have stored one date '22/7/2009'.Now i want to convert value in session to datetime. I tried folowing code:

Session("AppointmentDate") = Request.QueryString("ADate")
Dim s as datetime=Convert.ToDateTime(Session("AppointmentDate"))

But error is showing as 'string is not recognized as a valid datetime'. Can anybody help me to convert value in Session to date?

A: 

I think you need to store your date as 22/07/2009 so that Convert.ToDateTime() could recognize it.

RaYell
+1  A: 

Use DateTime.ParseExact to specify the date format string. I think it'd be something like:

 dateValue = Date.ParseExact(dateString, "d", frFR, DateTimeStyles.None)

or

 dateValue = Date.ParseExact(dateString, "dd/M/yyyy", enUS, DateTimeStyles.None)
Jon Galloway
+3  A: 

Why don't you put the value into the session as a DateTime?

Failing that, use :-

Use :-

DateTime.ParseExact(
  myDateString, 
  "d/M/yyyy",     
  System.Threading.Thread.CurrentThread.CurrentCulture);

Where myDateString contains your value from Session["AppointmentDate"]

Paul Alan Taylor
Technically, your fist suggestion just moves the issue up a bit, if the querystring is "22/7/2009" then calling Convert.ToDateTime(Request.QueryString("ADate")) wouldn't be any different ;)
Zhaph - Ben Duguid
Agreed :) Gotta parse sometime.
Paul Alan Taylor