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

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

问题描述

如何使用 NSDate 或 Cocoa touch 中的类似内容获取当前月份的当前天数?

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

推荐答案

您可以使用 NSDateNSCalendar 类:

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 是一个表示当前日期的 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 将包含当月的天数.

这里是 NSDateNSCalendar 如果您想了解更多信息.

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

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

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