<p>For example, with an invalid date like <code>11/31/15</code>, I want to get an error saying that there are only 30 days in November, but the <code>time.Parse</code> function accepts it anyway and parses it to December 1st.</p>
<p>Here's my code: <a href="http://play.golang.org/p/7iHg30Lpo4">http://play.golang.org/p/7iHg30Lpo4</a></p>
<hr/>**评论:**<br/><br/>IntellectualReserve: <pre><p>You could format the parsed date into a string.</p>
<p>If original string != formatted string, then the date is invalid. </p>
<p>Kinda hacky, I know, but it solves your problem today. :)</p></pre>leakyshadows: <pre><p>I thought of that but I was wondering if I was missing something since it seems like such a simple thing.</p>
<p>Anyway, in case someone else stumbles on this page, here's the code I ended up using:</p>
<p><a href="http://play.golang.org/p/tWAVrtVMVK">http://play.golang.org/p/tWAVrtVMVK</a></p></pre>IntellectualReserve: <pre><p>The edge case you might have to worry about is if you have multiple input formats. E.g. 1/2/06 vs 01/02/06. </p></pre>sajal: <pre><p>you should open issue in Go's github repo and propose your solution... i would like to see parsestrict in time package...</p></pre>captainju: <pre><p>Have you tried specifying a location ?
<a href="https://www.godoc.org/time#ParseInLocation" rel="nofollow">https://www.godoc.org/time#ParseInLocation</a></p></pre>inanotherworld: <pre><p>I'm interested in a clean solution to this problem as well.</p>
<p>I don't know why it was designed that way, but it's annoying and goes against the strictness we see everywhere else in go.</p></pre>IntellectualReserve: <pre><p>I don't know if <a href="http://www.reddit.com/r/golang/comments/3tw4te/how_to_validate_a_date_string/cx9ryed" rel="nofollow">my solution</a> meets your definition of clean, but it's there. :)</p></pre>Niriel: <pre><p>For most of Europe, 11/31/15 means day 11 of month 31. Although Hungary uses 2000-12-31 which is pretty much the ISO standard. Also, leap years.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传