可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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:
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();
回答2:
Yes. ISO 8601
http://en.wikipedia.org/wiki/ISO_8601
Just make sure you URLEncode it to deal with the colons.
回答3:
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.
回答4:
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.
回答5:
Try
HttpServerUtility.HtmlEncode
and
HttpServerUtility.HtmlDecode
回答6:
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! :-)
回答7:
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.
回答8:
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.