ios - 在 Objective-c 中检查时间?
<p><p>如何以 hh:mm 格式获取当前时间?我需要能够分辨上午和下午,并比较当前时间和第二次时间。我确定这是一个愚蠢的功能,但我似乎无法弄清楚。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>当前日期和日期比较:</p>
<pre><code>NSDate * now = ;
NSDate * mile = [ initWithString:@"2001-03-24 10:45:32 +0600"];
NSComparisonResult result = ;
NSLog(@"%@", now);
NSLog(@"%@", mile);
switch (result)
{
case NSOrderedAscending: NSLog(@"%@ is in future from %@", mile, now); break;
case NSOrderedDescending: NSLog(@"%@ is in past from %@", mile, now); break;
case NSOrderedSame: NSLog(@"%@ is the same as %@", mile, now); break;
default: NSLog(@"erorr dates %@, %@", mile, now); break;
}
;
</code></pre>
<p>对于日期格式,有一个 <code>NSDateFormatter</code>。您可以在 <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html" rel="noreferrer noopener nofollow">Date and Time programming guide for cocoa</a> 中找到更多信息和 <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369" rel="noreferrer noopener nofollow">Date formatters</a>在数据格式指南中,来自链接的示例:</p>
<pre><code>NSDateFormatter *inputFormatter = [ init];
;
NSDate *formatterDate = ;
NSDateFormatter *outputFormatter = [ init];
;
NSString *newDateString = ;
NSLog(@"newDateString %@", newDateString);
// For US English, the output is:
// newDateString 10:30 on Sunday July 11
</code></pre></p>
<p style="font-size: 20px;">关于ios - 在 Objective-c 中检查时间?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/2576130/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/2576130/
</a>
</p>
页:
[1]