Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 2 Beta: DateTime conversion. Bug or not?

Tags:

<%= Html.ActionLink(Html.Encode("user3"),
  "Filter", new { controller = "Search",
   userName = "user3", 
   dateFrom = DateTime.Now.AddDays(-2).ToString(), 
   dateTo = DateTime.Now.ToString() })%>

It's evaluated to this link:

http://localhost:60357/Search/Filter?userName=user3&dateFrom=08.02.2010%2023%3A21%3A33&dateTo=10.02.2010%2023%3A21%3A33

I have to notice that current Globalization settings in Windows are set up to european date format. So 08.02.2010 = 08 Feb 2010.

But in the action method

public ActionResult Filter(String userName, DateTime dateFrom, DateTime dateTo)

dateFrom has the value 02.08.2010 = 02 Aug 2010

It's incorrect. Is it framework bug? So what is the best way to solve this issue? I don't want to write some monkeypatch if possible.

like image 968
Overdose Avatar asked Feb 10 '10 21:02

Overdose


1 Answers

Don't use DateTime.Now.ToString(). Use DateTime.Now.ToString("s") (ISO 8601 format -- yyyy-MM-ddTHH:mm:ss) There is only one possible way to interpret that, so it's never wrong -- at least, until you start dealing with time zones! (Consider "u" format for that.)

like image 71
Craig Stuntz Avatar answered Oct 12 '22 23:10

Craig Stuntz