当月使用iPhone SDK的天数? [英] Number of days in the current month using iPhone SDK?

查看:94
本文介绍了当月使用iPhone SDK的天数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个相当直接的问题,我想知道如何使用NSDate或类似的Cocoa touch获取当前月份的当前天数。感谢。

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.

推荐答案

您可以使用 NSDate NSCalendar 类:

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];

今天 NSDate 表示当前日期的对象;这可以用于计算当月的天数。然后实例化 NSCalendar 对象,它可以与当前日期的 NSDate 结合使用,返回使用 rangeOfUnit:inUnit:forDate:函数在当月的天数。

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 将包含当前月份的天数。

days.length will contain the number of days in the current month.

这里是 NSDate NSCalendar code> 如果您想要更多信息。

Here are the links to the docs for NSDate and NSCalendar if you want more information.

这篇关于当月使用iPhone SDK的天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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