菜鸟教程小白 发表于 2022-12-12 22:56:39

ios - NSDateFormatter 的 NSDate 输出不同


                                            <p><p>我在使用 <code>NSDate</code> 时遇到了一些问题,实际上我只需要它来验证它是否是有效日期,因为如果它是无效日期,则 <code>date</code> 实例将返回零。但是</p>

<pre><code>NSString *textValue = @&#34;13/12/2014&#34;;

NSDateFormatter *formatter = [ init];
;

// this should cause the output to have no time
;
formatter.locale = [ initWithLocaleIdentifier:@&#34;en_US&#34;];
initWithCalendarIdentifier:NSGregorianCalendar]];
];

NSDate *date = ;
</code></pre>

<p><code>date</code>的结果是<code>2015-01-12 00:00:00 +0000</code></p>

<p>我期望的结果应该是:<code>nil</code></p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><pre><code>NSString *textValue = @&#34;1/12/2014&#34;;
NSString *textValue1 = @&#34;15/12/2014&#34;;
NSDateFormatter *formatter = [ init];
;
NSDate *date = ;
NSLog(@&#34; &gt;&gt; %@ &#34;,date);
NSDate *date1 = ;
NSLog(@&#34; &gt;&gt; %@ &#34;,date);
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - NSDateFormatter 的 NSDate 输出不同,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/24134322/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/24134322/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - NSDateFormatter 的 NSDate 输出不同