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.
You could pass a date in the query string using a specific format, say yyyymmdd and then parse it correctly in your Controller.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With