Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 243
Daniel Schaffer Avatar asked Feb 26 '10 21:02

Daniel Schaffer


People also ask

Can we pass date in URL?

You could pass a date in the query string using a specific format, say yyyymmdd and then parse it correctly in your Controller.

What is universal date/time format?

Times are expressed in UTC (Coordinated Universal Time), with a special UTC designator ("Z"). Times are expressed in local time, together with a time zone offset in hours and minutes. A time zone offset of "+hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes ahead of UTC.

What is the standard time format?

ISO 8601 is an international standard covering the worldwide exchange and communication of date and time-related data. It is maintained by the Geneva-based International Organization for Standardization (ISO) and was first published in 1988, with updates in 1991, 2000, 2004, and 2019.

What is the default DateTime format?

uses a default date format to store and manipulate strings that represent dates. To specify the default date format, enter a date format in the DateTime Format String attribute in the data viewer configuration. By default, the date format is MM/DD/YYYY HH24:MI:SS.US.


2 Answers

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();
like image 93
Jeffrey L Whitledge Avatar answered Sep 18 '22 20:09

Jeffrey L Whitledge


Yes. ISO 8601

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

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

like image 27
Jonathan Allen Avatar answered Sep 19 '22 20:09

Jonathan Allen