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

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

问题描述

一个相当直截了当的问题,我试图弄清楚如何使用NSDate或类似Cocoa touch中的类似东西来获取当前月份的当前天数。谢谢。

解决方案

您可以使用 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屋!

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