使用iOS在当月的天数是多少? [英] Number of days in the current month using iOS?

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

问题描述

如何使用NSDate或可可触摸中的类似方法获得当月的当前天数?

How can I get the current number of days in the current month using NSDate or something similar in Cocoa touch?

推荐答案

您可以使用 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 将包含当月的天数。

这里是 NSDate NSCalendar ,如果您需要更多信息。

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

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

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