Click to See Complete Forum and Search --> : [RESOLVED] Date Time Manipulation


bulgarian388
01-21-2007, 07:08 PM
Hi dudes, I have a bit of a problem that has been pissing me off for the past hour now. I have a form that posts back to its' self to view a traffic log. In the form are two fields, "startDate" and "endDate". The data is stored in MySQL so I have to convert the user input to something MySQL likes:

1/1/2007 = 2007-01-01

Problem is that I'm not getting anywhere with it. Here is the code I'm using:


String startDate = Request.Form["startDate"];
if (startDate == null) {
DateTime tempDate = DateTime.Now.AddDays(-1);
} else {
DateTime tempDate = Convert.ToDateTime(startDate);
}
startDate = Convert.ToString(tempDate.Year + "-" + tempDate.Month + "-" + tempDate.Day);


I keep getting this error when I try to run it:

"CS0103: The name 'tempDate' does not exist in the current context"

It's driving me insane right now. I am trying to port it over from ASP classic where I just did this:


startDate = request.Form("startDate")
if startDate = "" or isnull(startDate) then
startDate = dateAdd("d", -1, date())
end if
startDate = year(startDate) & "-" & month(startDate) & "-" & day(startDate)


If anyone knows the answer, I would really appreciate it if you could let me know.

Thanks in advance.

Chitra Xavier
01-21-2007, 11:45 PM
Hi....

Scope of tempDate variable is limited with in conditional loop.
Declare tempDate outside if loop.
Hope this will solve ur problem

bulgarian388
01-22-2007, 02:55 PM
Well, what do you know, it works! :) Thank you for the help good sir. :)

Ribeyed
01-22-2007, 05:54 PM
You can also do this:


Dim StartDate As System.DateTime = DateTime.Parse(StartDate.Text, System.Globalization.CultureInfo.CreateSpecificCulture("en-AU").DateTimeFormat)