开发者

asp.net mvc: date comparison fails on production server

开发者 https://www.devze.com 2022-12-16 07:51 出处:网络
I have a simple comparison in my view to see if an event is in the past: <% if (model.EventDate < DateTime.Now)

I have a simple comparison in my view to see if an event is in the past:

  <% if (model.EventDate < DateTime.Now)
     { %>
  <td style="color: red;">
  <% }
     else
     { %>
    <td>
  <% } %>

This works fine on my dev machine, running via Cassini, but on the server it seems to be interpreting 01/12/2010 as Dec. 1, not Jan 12.

How should I be doing this comparison to make sure that it works the same regardless of the runtime environment?

Update: The EventDate is a DateTime, and is coming from 开发者_JAVA技巧a database, which has the correct date: select MONTH(EventDate) returns 1, select DAY(EventDate) returns 12.


Comparing two DateTime instances in .NET always works. The problem lies when you set model.EventDate, maybe you are parsing it from a string.


The server is running under a different culture than your dev box. This is one of the reasons why people should save dates as UTC.


As a work around you could try adding the following into your web.config (it's a child of <system.web>):

<globalization culture="en-US" />

HTHs,
Charles

0

精彩评论

暂无评论...
验证码 换一张
取 消