开发者

Is there a standard date/time format that can be passed on a URL?

Looking at the DateTimeFormatInfo doc开发者_运维问答umentation, 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.


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();


Yes. ISO 8601

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

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


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.


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.


Try HttpServerUtility.HtmlEncode and HttpServerUtility.HtmlDecode


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! :-)


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.


I've just come across this same problem and solved it with the following helper method:

    private string Encode(DateTime date)
    {
        var formattedDate = date.ToString("yyyy-MM-ddTHH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
        return WebUtility.UrlEncode(formattedDate);
    }

After using the output in a URL query parameter the value gets correctly deserialised by .NET into DateTime objects passed into my Web API Service.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜