Passing a C# DateTime via the Query String

Just use ToString() and pass a format e.g.: startDate.ToString("yyyyMMddHHmmss")

And parse it back by using DateTime.ParseExact()


For accuracy and consistency you could use:

string utcDateOut = DateTime.UtcNow.ToString("s", CultureInfo.InvariantCulture);

DateTime utcDateIn = DateTime.ParseExact(utcDateOut, "s", 
                              CultureInfo.InvariantCulture, 
                              DateTimeStyles.AdjustToUniversal);

This will give you an ISO 8601 compliant format and the use of UTC will ensure that there are no issues with time zones etc.

Only drawback is that it doesn't look as "nice" as a simple "yyyyMMdd".


I'd use yyyyMMdd as the format; doesn't need to be URL encoded and it's easy to read/understand.

On the server side, you'd have to call DateTime.ParseExact(dateString, "yyyyMMdd") to get the date out.

Hope this helps.

Tags:

C#

.Net

Datetime