如何获取当前小时使用Cocoa? [英] How do I get the current hour using Cocoa?

查看:112
本文介绍了如何获取当前小时使用Cocoa?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Objective-C获取Cocoa的当前时间?

How do I get the current hour in Cocoa using Objective-C?

推荐答案

a href =http://developer.apple.com/documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html#//apple_ref/doc/uid/10000039 =nofollow noreferrer>可可的日期和时间编程主题。这将让您很好地了解使用Cocoa中提供的各种日期/时间/日历对象进行日期的高级转换。

To start off, you should read Dates and Times Programming Topics for Cocoa. That will give you a good understanding of using the various date/time/calendar objects that are provided in Cocoa for high-level conversions of dates.

但是,此代码snip ,将回答您的具体问题:

This code snip, however, will answer your specific problem:

- (NSInteger)currentHour
{
    // In practice, these calls can be combined
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSHourCalendarUnit fromDate:now];

    return [components hour];
}

这篇关于如何获取当前小时使用Cocoa?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆