对日期和时间的处理 NSCalendar

代码较老,供参考

NSCalendar用于处理时间相关问题。比如比较时间前后、计算日期所的周别等。

1. 创建或初始化可用以下方法

+ (id)currentCalendar;

取得当前用户的逻辑日历(logical calendar)

+ (id)autoupdatingCurrentCalendar;

取得当前用户的逻辑日历(logical calendar), ......

- (id)initWithCalendarIdentifier:(NSString *)identifier;

初始化为各种日历。identifier的范围可以是:

NSGregorianCalendar 阳历

NSBuddhistCalendar 佛历

NSChineseCalendar 中国日历

NSHebrewCalendar 希伯来日历

NSIslamicCalendar 伊斯兰日历

NSIslamicCivilCalendar 伊斯兰民事日历

NSJapaneseCalendar 日本日历

2. 使用前若有必要可以先做以下设定

- (void)setLocale:(NSLocale *)locale;

设置区域

- (void)setTimeZone:(NSTimeZone *)tz;

设置时区

- (void)setFirstWeekday:(NSUInteger)value;

设定每周的第一天从星期几开始,比如:

.  如需设定从星期日开始,则value传入1

.  如需设定从星期一开始,则value传入2

.  以此类推

详细信息请参阅FirstWeekday属性

- (void)setMinimumDaysInFirstWeek:(NSUInteger)value;

设定作为(每年及每月)第一周必须包含的最少天数,比如:

.  如需设定第一周最少包括7天,则value传入7

详细信息请参阅MinimumDaysInFirstWeek属性

3. 取回设定信息用以下方法

- (NSString *)calendarIdentifier

返回日历标示符(identifier)。有效的日历标示符包括:

NSGregorianCalendar

NSBuddhistCalendar

NSChineseCalendar

NSHebrewCalendar

NSIslamicCalendar

NSIslamicCivilCalendar

NSJapaneseCalendar

- (NSLocale *)locale;

返回日历指定的地区信息。它将影响???

- (NSTimeZone *)timeZone;

返回日历指定的时区信息。它将影响???

- (NSUInteger)firstWeekday;

返回日历指定的每周的第一天从星期几开始。缺省为星期天,即firstWeekday = 1

当方法[NSCalendar ordinalityOfUnit: inUnit: fromDate:]

的ordinalityOfUnit参数为NSWeekdayCalendarUnit,inUnit参数为NSWeekCalendarUnit时,

firstWeekday属性影响它的返回值。具体说明如下:

.  当firstWeekday被指定为星期天(即 = 1)时,它返回的值与星期几对应的数值保持一致。比如:

fromDate传入的参数是星期日,则函数返回1

fromDate传入的参数是星期一,则函数返回2

.  当firstWeekday被指定为其它值时(即 <> 1)时,假设firstWeekday被指定为星期一(即 = 2),那么:

fromDate传入的参数是星期一,则函数返回1

fromDate传入的参数是星期二,则函数返回2

fromDate传入的参数是星期日,则函数返回7

- (NSUInteger)minimumDaysInFirstWeek;

返回日历指定的第一周必须包含的最少天数。缺省为1,即minimumDaysInFirstWeek = 1。

当方法[NSCalendar ordinalityOfUnit: inUnit: fromDate:]

的ordinalityOfUnit参数为NSWeekCalendarUnit,inUnit参数为NSYearCalendarUnit时,

minimumDaysInFirstWeek属性影响它的返回值。具体说明如下:

2005年1月

日   一   二   三   四   五   六

--------------------------------

1

2    3    4    5    6    7    8

9   10   11   12   13   14   15

16   17   18   19   20   21   22

23   24   25   26   27   28   29

30   31

2005年1月第一周包括1号。

a. 如果将minimumDaysInFirstWeek设定 = 1

则fromDate传入1月1号,方法均返回1  ==> 满足minimumDaysInFirstWeek指定的天数(最少1天),所以方法将其归为2005年的第1周

则fromDate传入1月2-8号,方法均返回2

则fromDate传入1月9-15号,方法均返回3

......

b. 如果将minimumDaysInFirstWeek设定为 > 1,比如2

则fromDate传入1月1号,方法均返回53  ==> 不足2天,所以方法将其归为2004年的第53周

则fromDate传入1月2-8号,方法均返回1

则fromDate传入1月9-15号,方法均返回2

......

2008年1月

日   一   二   三   四   五   六

---------------------------------

1    2    3    4    5

6    7    8    9   10   11   12

13   14   15   16   17   18   19

20   21   22   23   24   25   26

27   28   29   30   31

2005年1月第一周包括1-5号共5天。

a. 如果将minimumDaysInFirstWeek设定为 <= 5时

则fromDate传入1月1-5号,方法均返回1  ==> 满足minimumDaysInFirstWeek指定的天数,所以方法将其归为2008年的第1周

则fromDate传入1月6-12号,方法均返回2

则fromDate传入1月13-19号,方法均返回3

......

b. 如果将minimumDaysInFirstWeek设定为 > 5,比如6

则fromDate传入1月1-5号,方法均返回53  ==> 当周不足6天,所以方法将其归为2007年的第53周

则fromDate传入1月2-8号,方法均返回1

则fromDate传入1月9-15号,方法均返回2

......

当方法[NSCalendar ordinalityOfUnit: inUnit: fromDate:]

的ordinalityOfUnit参数为NSWeekCalendarUnit,inUnit参数为NSMonthCalendarUnit时,

minimumDaysInFirstWeek属性影响它的返回值。以2008年4月为例, 具体说明如下:

2008年4月

日   一   二   三   四   五   六

---------------------------------

1    2    3    4    5

6    7    8    9   10   11   12

13   14   15   16   17   18   19

20   21   22   23   24   25   26

27   28   29   30

2008年4月第一周包括1、2、3、4、5号。

1. 如果将minimumDaysInFirstWeek设定为小于或等于5的数

则fromDate传入4月1-5号,方法均返回1

则fromDate传入4月6-12号,方法均返回2

则fromDate传入4月13-19号,方法均返回3

....

2. 如果将minimumDaysInFirstWeek设定为大于5的数

则fromDate传入1-5号,方法均返回0

则fromDate传入6-12号,方法均返回1

则fromDate传入13-19号,方法均返回2

....

其他方法的说明
    - (NSRange)maximumRangeOfUnit:(NSCalendarUnit)unit;
    返回单元的最大范围。就Gregorian来说有:
     NSEraCalendarUnit => 0 - 2
     NSYearCalendarUnit => 1 - 10000
     NSMonthCalendarUnit = 1 - 12
     NSDayCalendarUnit = 1 - 31
     NSHourCalendarUnit = 0 - 24
     NSMinuteCalendarUnit = 0 - 60
     NSSecondCalendarUnit = 0 - 60
     NSWeekCalendarUnit = 1 - 53
     NSWeekdayCalendarUnit = 1 - 7
     NSWeekdayOrdinalCalendarUnit = ? (2147483647 - 2147483647)
                                         (NSNotFound - NSNotFound)

- (NSRange)minimumRangeOfUnit:(NSCalendarUnit)unit;
    返回单元的最小范围。就Gregorian来说有:
     NSEraCalendarUnit => 0 - 2
     NSYearCalendarUnit => 1 - 10000
     NSMonthCalendarUnit = 1 - 12
     NSDayCalendarUnit = 1 - 28  ***
     NSHourCalendarUnit = 0 - 24
     NSMinuteCalendarUnit = 0 - 60
     NSSecondCalendarUnit = 0 - 60
     NSWeekCalendarUnit = 1 - 52  ***
     NSWeekdayCalendarUnit = 1 - 7
     NSWeekdayOrdinalCalendarUnit = ? (2147483647 - 2147483647)
                                         (NSNotFound - NSNotFound)

- (NSUInteger)ordinalityOfUnit:(NSCalendarUnit)smaller inUnit:(NSCalendarUnit)larger forDate:(NSDate *)date
    返回某个特定时间(date)其对应的小的时间单元(smaller)在大的时间单元(larger)中的顺序,比如:
    . 要取得2008/11/12在当月的第几周则可以这样调用该方法:
      [calendar ordinalityOfUnit:NSWeekOrdinalCalendarUnit inUnit: NSWeekCalendarUnit forDate: someDate]; 
      注: someDate存放了2008/11/12

. 要取得1:50分在一天中处于第几个小时,则可以这样调用该方法:
      [calendar ordinalityOfUnit:NSHourCalendarUnit inUnit: NSDayCalendarUnit forDate: someTime]; 
      注: someTime存放了1:50

. 其它请参照firstWeekday和minimumDaysInFirstWeek中的说明

- (NSRange)rangeOfUnit:(NSCalendarUnit)smaller inUnit:(NSCalendarUnit)larger forDate:(NSDate *)date
    返回某个特定时间(date)其对应的小的时间单元(smaller)在大的时间单元(larger)中的范围,比如:
    . 要取得2008/11/12在所在月份的日期范围则可以这样调用该方法:
      [calendar ordinalityOfUnit:NSDayCalendarUnit inUnit: NSMonthCalendarUnit forDate:fDate]; 
      则返回1-31。注: fDate存放了2008/11/12

. 要取得2008/02/20在所在月份的日期范围则可以这样调用该方法:
      [calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:fDate]; 
      则返回1-29。注: fDate存放了2008/11/12

- (BOOL)rangeOfUnit:(NSCalendarUnit)unit startDate:(NSDate **)sDate interval:(NSTimeInterval *)unitSecs forDate:(NSDate *)date;
    用于返回日期date(参数)所在的那个日历单元unit(参数)的开始时间(sDate)。其中参数unit指定了日历单元,参数sDate用于返回日历单元的第一天,参数unitSecs用于返回日历单元的长度(以秒为单位),参数date指定了一个特定的日期。
    如果startDate和interval均为可计算的,那么函数返回YES否则返回NO.比如:

. 要计算2008/01/08日所在的周的第一天:
        ...
        NSDate *dateOut = [NSDate date];
        NSDate **sDate = &dateOut;
 
        NSDateComponents *dc = [[NSDateComponents alloc] init];
        [dc setYear: 2008];
        [dc setMonth: 1];
        [dc setDay: 8];
       
        NSDate *date = [calendar dateFromComponents:dc];
        NSTimeInterval intOut;
        NSTimeInterval *intervalOut = &intOut;
 
        if ([calendar rangeOfUnit:NSMonthCalendarUnit startDate:sDate interval:interval0 forDate:date]){
            NSLog(@"%@", [*sDate description]);
            NSLog(@"%f", *intervalOut);
        }
        else{
            NSLog(@"Can not be calculated!");
        }
        ...
      在控制台窗口上可以看到:
      2008-01-06 00:00:00 +0800
      604800.000000
      注: 2008-01-06是2008/01/08所在周的第一天,604800是一周7天的秒数。

. 要计算2008/01/05日所在的周的第一天,在控制台窗口上可以看到:
      2007-12-30 00:00:00 +0800
      604800.000000

. 要计算2008/02/06日所在的月的第一天,在控制台窗口上可以看到:
      2007-02-01 00:00:00 +0800
      2505600.000000
      注: 2505600是二月29天的秒数。

时间: 2024-08-02 13:53:19

对日期和时间的处理 NSCalendar的相关文章

Cocoa中对日期和时间的处理 NSCalendar (一)

NSCalendar用于处理时间相关问题.比如比较时间前后.计算日期所的周别等. 1. 创建或初始化可用以下方法     + (id)currentCalendar; 取得当前用户的逻辑日历(logical calendar)     + (id)autoupdatingCurrentCalendar; 取得当前用户的逻辑日历(logical calendar), ......     - (id)initWithCalendarIdentifier:(NSString *)identifier

日期和时间的处理 NSCalendar (二)

其他方法的说明    - (NSRange)maximumRangeOfUnit:(NSCalendarUnit)unit;    返回单元的最大范围.就Gregorian来说有:     NSEraCalendarUnit => 0 - 2     NSYearCalendarUnit => 1 - 10000     NSMonthCalendarUnit = 1 - 12     NSDayCalendarUnit = 1 - 31     NSHourCalendarUnit = 0

我的OC成长之路(复习之日期与时间)

注意: 1.计算当月内的日历 计算一个月内总共有几天: // 获取当月的天数 - (NSInteger)getNumberOfDaysInMonth { NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; // 指定日历的算法 NSDate * currentDate = [NSDate date]; // 这个日期可以你自己给定 NSRange range =

取得日期、时间的各个位置的数字

直接看代码  最直接 //日期处理 NSDate *now = [NSDate date]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *comonents = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit fr

获取当前的日期和时间-数码

IOS---如何获取当前的日期和时间,并显示 创建两个UILable: Lable1,Lable2.创建好IBOutlate并连线, 然后用如下方法: -(void)updateLabel { NSDate* now = [NSDate date]; NSCalendar *cal = [NSCalendar currentCalendar]; unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCal

php日期和时间基础知识--Unix时间戳

<?php /*1.取得当前的Unix时间戳 UNIX 时间戳(英文叫做:timestamp)是 PHP 中关于时间与日期的一个很重要的概念,它表示从 1970年1月1日 00:00:00 到当前时间的秒数之和. PHP提供了内置函数 time() 来取得服务器当前时间的时间戳.那么获取当前的UNIX时间戳就很简单了. */ $timer = time(); echo $timer; /*2.取得当前的日期 php内置了date()函数,来取得当前的日期. 函数说明:date(时间戳的格式, 规

(十六)PL/SQL日期及时间

PL/SQL提供两个日期和时间相关的数据类型: 1.日期时间(Datetime)数据类型 DATE TIMESTAMP TIMESTAMP WITH TIME ZONE TIMESTAMP WITH LOCAL TIME ZONE 2.间隔数据类型 INTERVAL YEAR TO MONTH INTERVAL DAY TO SECOND   一.日期时间字段值和间隔数据类型这两个日期时间和间隔数据类型包括字段.这些字段的值确定的数据类型的值.下表列出了时间和间隔的字段及其可能的值. 字段名称

前端学PHP之日期与时间

前面的话 在Web程序开发时,时间发挥着重要的作用,不仅在数据存储和显示时需要日期和时间的参与,好多功能模块的开发,时间通常都是至关重要的.网页静态化需要判断缓存时间.页面访问消耗的时间需要计算.根据不同的时间段提供不同的业务等都离不开时间.PHP为我们提供了强大的日期和时间处理功能,通过内置的时间和日期函数库,不仅能够得到PHP程序在运行时所在服务器中的日期和时间,还可以对它们进行任意检查和格式化,以及在不同格式之间进行转换等.本文将详细介绍PHP中的日期和时间 [注意]关于javascrip

使用moment.js轻松管理日期和时间

大家在前端Javascript开发中会遇到处理日期时间的问题,经常会拿来一大堆处理函数才能完成一个简单的日期时间显示效果.今天我给大家介绍一个轻量级的Javascript日期处理类库:moment.js,使用它可以轻松解决前端开发中遇到的种种日期时间问题. 查看演示 下载源码 moment.js不依赖任何第三方库,支持字符串.Date.时间戳以及数组等格式,可以像PHP的date()函数一样,格式化日期时间,计算相对时间,获取特定时间后的日期时间等等,本文有如下举例. 格式化日期 当前时间: m