有可以在URL上传递的标准日期/时间格式吗? [英] Is there a standard date/time format that can be passed on a URL?

查看:1280
本文介绍了有可以在URL上传递的标准日期/时间格式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看 DateTimeFormatInfo 文档,它似乎所有的标准格式都有冒号,这使得它们传递给url不愉快/不可能。



有没有标准的格式来传递一个URL的日期时间,最好是由.NET自动解析的?






更新: / p>

这个数据的消费者将是某种类型的Web服务 - 它将是一个简单的HTTP GET,在查询字符串中具有此值,或者将REST的值在某个地方的URL。



ISO 8601 管理日期/时间格式,根据wiki文章,使用 ToString(yyyyMMddTHHmmssZ)应该是st至少符合标准。不幸的是,它不会被ASP.NET MVC自动拾取(还没有尝试任何其他的东西)。对于什么是值得的,ASP.NET MVC不会自动将ticks转换为datetime,这让我感到惊讶。

解决方案

将所有东西放在一起,成为一个答案:



你可以这样做:

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

或者你可以这样做:

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

DateTime date = DateTime.ParseExact(urlSafeDateString,yyyyMMddTHHmmss,System.Globalization.CultureInfo.InvariantCulture).ToLocalTime();


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.

解决方案

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

这篇关于有可以在URL上传递的标准日期/时间格式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆