Why don't you use strptime?<div><br></div><div><a href="http://www.ruby-doc.org/stdlib/libdoc/date/rdoc/classes/Date.html#M000357">http://www.ruby-doc.org/stdlib/libdoc/date/rdoc/classes/Date.html#M000357</a></div><div>
<br><br><div class="gmail_quote">On 26 July 2011 11:55, Paul Robinson <span dir="ltr"><<a href="mailto:paul@32moves.com">paul@32moves.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">On 26 Jul 2011, at 11:39, John Winters wrote:<br>
<br>
> 1) How exactly does Rails interpret a date string typed into a field? What method does it use?<br>
<br>
<br>
</div>I always assumed it simply hands it off to Date.parse, which in turn hands off to _parse:<br>
<br>
<a href="http://ruby-doc.org/stdlib/libdoc/date/rdoc/classes/Date.html#M000458" target="_blank">http://ruby-doc.org/stdlib/libdoc/date/rdoc/classes/Date.html#M000458</a><br>
<div class="im"><br>
<br>
> 2) How does one control this behaviour?<br>
<br>
<br>
</div>There are helpers, but if they are seeming a bit confusing or you need/want more control, ideally you want to control the input format better, and you should consider cleaning the user input into a coherent date format.<br>

<br>
For example, Date.parse is 100% consistent with YYYY-MM-DD, and you could therefore write a couple of methods to wrap around your actual_date field like this:<br>
<br>
def user_date<br>
  return actual_date.strftime("%d/%m/%Y") if current_user.british?<br>
  return actual_date.strftime("%m/%d/%Y") if current_user.american?<br>
  ...<br>
end<br>
<br>
def user_date=(date)<br>
  actual_date = Date.parse("#{date[6,4]}-#{date[3,2]}-#{date[0,2}") if current_user.british?<br>
  actual_date = Date.parse("#{date[6,4]}-#{date[0,2}-#{date[3,2]}") if current_user.american?<br>
  ...<br>
end<br>
<br>
In your forms, just reference user_date... It's an ugly bodge, but it'll work if the existing methods aren't what you're after.<br>
<font color="#888888"><br>
Paul<br>
</font><div><div></div><div class="h5">_______________________________________________<br>
Chat mailing list<br>
<a href="mailto:Chat@lists.lrug.org">Chat@lists.lrug.org</a><br>
<a href="http://lists.lrug.org/listinfo.cgi/chat-lrug.org" target="_blank">http://lists.lrug.org/listinfo.cgi/chat-lrug.org</a><br>
</div></div></blockquote></div><br></div>