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

前端 未结 8 1729
臣服心动
臣服心动 2021-01-01 13:51

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.

相关标签:
8条回答
  • 2021-01-01 14:31

    Try HttpServerUtility.HtmlEncode and HttpServerUtility.HtmlDecode

    0 讨论(0)
  • 2021-01-01 14:31

    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.

    0 讨论(0)
  • 2021-01-01 14:34

    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();
    
    0 讨论(0)
  • 2021-01-01 14:38

    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 讨论(0)
  • 2021-01-01 14:40

    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.

    0 讨论(0)
  • 2021-01-01 14:46

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

    0 讨论(0)
提交回复
热议问题