使用iPhone SDK的当前天数? [英] Number of days in the current month using iPhone SDK?
问题描述
您可以使用 NSDate
和 NSCalendar
类:
NSDate * today = [NSDate date] //获取今天日期的日期对象
NSCalendar * c = [NSCalendar currentCalendar];
NSRange days = [c rangeOfUnit:NSDayCalendarUnit
inUnit:NSMonthCalendarUnit
forDate:today];
今天
是一个 NSDate
表示当前日期的对象;这可以用来计算当月的天数。然后将 NSCalendar
对象实例化,可以与当前日期的 NSDate
一起使用,以返回当前月份的天数使用 rangeOfUnit:inUnit:forDate:
函数。
days.length
将包含当月的天数。
以下是 NSDate
和 NSCalendar
如果你想要更多的信息。
A fairly straight forward question, I am trying to figure out how I can get the current number of days in the current month using NSDate or something similar in Cocoa touch. Thanks.
You can use the NSDate
and NSCalendar
classes:
NSDate *today = [NSDate date]; //Get a date object for today's date
NSCalendar *c = [NSCalendar currentCalendar];
NSRange days = [c rangeOfUnit:NSDayCalendarUnit
inUnit:NSMonthCalendarUnit
forDate:today];
today
is an NSDate
object representing the current date; this can be used to work out the number of days in the current month. An NSCalendar
object is then instantiated, which can be used, in conjunction with the NSDate
for the current date, to return the number of days in the current month using the rangeOfUnit:inUnit:forDate:
function.
days.length
will contain the number of days in the current month.
Here are the links to the docs for NSDate
and NSCalendar
if you want more information.
这篇关于使用iPhone SDK的当前天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!