tags:

views:

155

answers:

7

Looking at the DateTimeFormatInfo documentation, it appears that all the standard formats have colons in them, which makes passing them on a url unpleasant/impossible.

Is there a standardized format for passing a datetime on a url, preferably one that can be automatically parsed by .NET?


Update: A little clarification

The consumer of this data is going to be web service of some kind - it'll either be a simple HTTP GET with this value in the querystring, or it'll be REST with the value in the url somewhere.

ISO 8601 governs date/time formatting, and according to the wiki article, using ToString("yyyyMMddTHHmmssZ") should be standards-compliant at least. Unfortunately, it doesn't get picked up automatically by ASP.NET MVC (haven't tried anything else yet). For what it's worth, ASP.NET MVC won't automatically convert ticks to a datetime either, which surprised me.

+1  A: 

It's probably not part of any standard or anything like that, but you could always use a Unix timestamp as those only contain digits. See this article for ways to convert it to a .NET DateTime.

Xeon06
A: 

Try HttpServerUtility.HtmlEncode and HttpServerUtility.HtmlDecode

madatanic
The static class is `HttpUtility`, and it would be `HttpUtility.UrlEncode`, not html encoding for a url...
Daniel Schaffer
A: 

We actually have a reporting solution where dates and times can be passed in as plain text CCYYMMDD and hhmmss values then, when the code needs to do anything to those values, it simply uses string operations to turn them back into a format that can be displayed or easily parsed (such as the DB2 ISO formats CCYY-MM-DD or hh.mm.ss).

The result is readable and I'd be surprised if .NET didn't have string manipulation instructions that could do this easily.

paxdiablo
A: 

If you need the date and time, I would use DateTime.Ticks.

If you only need the date, use HttpServerUtility.UrlEncode(date.ToShortDateString()).

Update: Rick Strahl has discussed this issue before and has offered a solution on his blog.

jrummell
Be sure to use the InvariantCulture (on both client and server) or this will work/fail at random for different users!
Jeffrey L Whitledge
HtmlEncode? this is going on a url...
Daniel Schaffer
I changed it to UrlEncode.
jrummell
+1  A: 

Yes. ISO 8601

http://en.wikipedia.org/wiki/ISO_8601

Just make sure you URLEncode it to deal with the colons.

Jonathan Allen
I get an `HTTP 400 BAD REQUEST` when I use UrlEncode, and also if I do it twice.
Daniel Schaffer
+1  A: 

I'll put everything together into one answer:

You could do something like this:

        string urlSafeDateString = HttpServerUtility.UrlTokenEncode(date.ToUniversalTime().ToString(System.Globalization.CultureInfo.InvariantCulture).ToCharArray());

        DateTime date = DateTime.Parse(new string(HttpServerUtility.UrlTokenDecode(urlSafeDateString)), System.Globalization.CultureInfo.InvariantCulture).ToLocalTime();

Or you could do something like this:

        string urlSafeDateString = date.ToUniversalTime().ToString("yyyyMMddTHHmmss", System.Globalization.CultureInfo.InvariantCulture);

        DateTime date = DateTime.ParseExact(urlSafeDateString, "yyyyMMddTHHmmss", System.Globalization.CultureInfo.InvariantCulture).ToLocalTime();
Jeffrey L Whitledge
This is pretty close to what I'm using for now.
Daniel Schaffer
A: 

For what it's worth, ASP.NET MVC won't automatically convert ticks to a datetime either, which surprised me.

There's a constructor for DateTime that takes the number of ticks:

 DateTime d = new DateTime(634028202671420663);

There's also an override that lets you map that to either local or UTC time. It's all in the docs: http://msdn.microsoft.com/en-us/library/system.datetime.datetime.aspx

Edit: realised from the wording of the post that perhaps you're talking about ASP converting the ticks automatically to a DateTime, rather than having to read the numeric parameter. Perhaps! :-)

Dan Puzey
Yeah, I was hoping to find a format that would work automatically (for use with model binding)
Daniel Schaffer