tags:

views:

238

answers:

7

i have a query string with format MM/DD/YYYY

I am using it in c# like

DateTime d = Request.QueryString["dateTime"].toString();

its giving me a lot of error saying the date time format is not recognized. If i manually change the datetime in browser address bar (query string) to dd/mm/yyyy then the program just works fine.

I cannot change the query string, is there a way in c# to get it from browser and then convert into date like dd/mm/yyyy please?

edit: the query string:

http://localhost:49543/HM/Admin/ViewDetails.aspx?OrderNo=10&DateCreated=08/30/2010

so you can see the datecreated part is in MM/DD/YYYY format. I am not able to grab it from c#. If I manually change that to 30/08/2010, it works

+1  A: 
// Parsing:
DateTime d = DateTime.Parse(Request.QueryString["dateTime"].toString());

// Conversion:
string dString = d.ToWhateverFormatYouWant();

And here's some info on formatting dates:

http://msdn.microsoft.com/en-us/library/az4se3k1(VS.71).aspx

Ocelot20
i want it in dateTime variable type itself!
locusthorde
+5  A: 

How to turn string from request into DateTime:

DateTime d = DateTime.ParseExact(Request.QueryString["dateTime"], "dd/MM/yyyy", null);
Timwi
the first one, I tried it, it says that the iFormatProvider to String cannot be done manually. its throwing 2 errors on that line!(and I want the first case scenario - sorry if my question was confusing!)
locusthorde
@locusthorde: OK, sorry for posting without trying it. I changed the answer to something that works for me. (It will still throw an exception if the input string is not *precisely* in this dd/MM/yyyy format. You might want to catch this exception and handle it appropriately.)
Timwi
+6  A: 
DateTime d = DateTime.ParseExact(Request.QueryString["dateTime"], "dd/MM/yyyy", CultureInfo.InvariantCulture);
Thomas Levesque
its still saying string not recognised as valid format!!!
locusthorde
I don't think he means that that is how the string is formatted, but rather that is what he wants to create. He needs to post the example QS.
Kyle Rozendo
locusthorde
locusthorde
@locusthorde, I fixed it. The 'yyyy' had to be lowercase, not uppercase as I had first written it
Thomas Levesque
the QS is in the "MM/dd/yyyy" format (says OP), not "dd/MM/yyyy".
Hans Kesting
Hi Thomas, now there is a new error: String reference not set to an instance of a String.Parameter name: ss is not even in the string! I am completely lost !
locusthorde
@Hans Kesting, yes Sir, I know that, I want to convert it into DateTime Variable, with dd/MM/yyyy format. thats the trouble!
locusthorde
@locusthorde, "s" is the name of the first parameter to the ParseExact method. You must be passing it a null string. You need to check that the "dateTime" query parameter is present before you try to convert it
Thomas Levesque
+1  A: 

DateTime.TryParse could be a great option..

Marco
+1  A: 

DateTime.ParseExact is the solution you seek for. But I recommend you to validate the querystring data with a function as follows:

bool isValidDate(string dtStr) {
    string pattern = @"^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[2][0]\d{2})$)";
    System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex(pattern);
    return re.IsMatch(dtStr);
}

EDIT 1: Besides ParseExact, you can use the following:

DateTime.Parse(dateString, new System.Globalization.CultureInfo("tr-TR"))

Turkish datetime format is dd/MM/YYYY.

Zafer
A: 

(DateTime.Now).ToString("dd/MM/yyyy");

Martin Ongtangco
A: 

Try this it should work

    DateTime d = 
           DateTime.ParseExact(Request.QueryString["dateTime"], 
           "dd'/'MM'/'yyyy",    
           CultureInfo.InvariantCulture);

I faced something similar: http://stackoverflow.com/questions/785795/datetime-format-in-c

Prashant