Changing string to datetime snafu

  • Thread starter Thread starter .Net Sports
  • Start date Start date
N

.Net Sports

Before processing my data in a datagrid, I need to parse the day of the
week (which will be my 'rqsday' variable) from a string that comes over
on a querystring:

string rqs = Request.QueryString["txtboarddate"];

string rqsday;

rqsday = (System.String.Format("{0:dddd}",rqs)) ;

Response.Write(rqsday);

...the variable 'txtboarddate' comes over as "7/14/2005 ) (or whatever
date the user is querying) . But I think I'm having trouble with 'rqs'
, as I can't find a way to change that to DATETIME datatype.

???
..netsports
 
If the date is coming in as a string in the format of MM/DD/YYYY, simply call
DateTime.Parse() passing in your date string to return an actual instance of
a DateTime with that date.

Brendan
 
Brendan, but how?
if I have
string rqs = Request.QueryString["txtboardd­ate"];

and then i try rqs = DateTime.Parse();

what am I "Parsing?" How would that be coded so I can turn it into a
datetime object so I can parse the day of the week from it
 
You would actually change the types you are using, instead of rsqday being a
string, you use it as a DateTime and have it reference the result of the
DateTime.Parse() of rsq ala:

string rqs = Request.QueryString["txtboarddate"];

DateTime rqsday;

rqsday = DateTime.Parse(rsq);

Response.Write(rqsday.ToString());


What you are doing is having the DateTime class parse the string you
returned from the database to determine the date that the string represents.

Brendan
 
Thanks for helping - still, I need to make rqsday into a string that
contains "Friday" or "Thursday" ( whatever day that the 'rqs' falls on)
..
Doing this:
string rqs = Request.QueryString["txtboardd­ate"];
DateTime rqsday;
rqsday = DateTime.Parse(rsq);
rqsday = rqsday.ToString("{dddd}");

...I still get the Build error pointing to the last line rqsday =
rqsday.ToString("{dddd}");
the "cannot implicitly convert type 'string' to 'System.DateTime' error

????
..netsports
 
So what you are looking for is the day of the week that the date returned
from Request.QueryString["txtboarddate"]? Ahh, I thought you were looking for
the full DateTime class. In either case, take the following code...

string dayOfWeek;
string rqs = Request.QueryString["txtboarddate"];
DateTime rqsday;
rqsday = DateTime.Parse(rqs);
dayOfWeek = rqsday.DayOfWeek.ToString();

One little note, I seem to have had a typo in the last code I gave you in
which rsq was being passed into DateTime.Parse(), that has been fixed now.

Anyway, here we get the string representing the actual date (ie 7/15/2005),
then have it parsed into an instance of the DateTime class, and finally read
the DayOfWeek property from that instance to determine the actual day of the
week, all without the need for the dddd format conversion to string. If
however you want to use that... you can simply change the associated line to:

dayOfWeek = rqsday.ToString("dddd");

or the following if you want the {} chars to be part of the output:

dayOfWeek = rqsday.ToString("{dddd}");

Remember though, that in either case a String is being returned so you
should be sure to store it in such a variable type, unlike the code you
replied with where you are attempting to store that string in rqsday which
has been changed to be a string, rather than a DateTime.

Brendan
 
..Net Sports said:
Thanks for helping - still, I need to make rqsday into a string that
contains "Friday" or "Thursday" ( whatever day that the 'rqs' falls on)
.
Doing this:
string rqs = Request.QueryString["txtboardd­ate"];
DateTime rqsday;
rqsday = DateTime.Parse(rsq);
rqsday = rqsday.ToString("{dddd}");

..I still get the Build error pointing to the last line rqsday =
rqsday.ToString("{dddd}");
the "cannot implicitly convert type 'string' to 'System.DateTime' error

????
.netsports

Firstly, if you call DateTime.ToString, that takes *just* a format
string, rather than a full string which *includes* a format string. In
other words, you want ToString("dddd") rather than ToString("{dddd}")

Secondly, you're trying to assign the formatted string to rqsday, which
is a DateTime. You need to use a string variable, eg rqs, instead.
 
Back
Top