分享

ios 时间的比较&&获得系统本地时间指定时区

 plumbiossom 2014-05-13

- (BOOL)isEqualToDate:(NSDate *)otherDate;

    与otherDate比较,相同返回YES


    - (NSDate *)earlierDate:(NSDate *)anotherDate;

    与anotherDate比较,返回较早的那个日期


    - (NSDate *)laterDate:(NSDate *)anotherDate;

    与anotherDate比较,返回较晚的那个日期



    - (NSComparisonResult)compare:(NSDate *)other;

    该方法用于排序时调用:

      . 当实例保存的日期值与anotherDate相同时返回NSOrderedSame

      . 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending

      . 当实例保存的日期值早于anotherDate时返回NSOrderedAscending

 // 获得本地时间指定时区
                    NSDate *dates = [NSDate date];
                    NSDateFormatter *formatter =  [[NSDateFormatter alloc] init];
                    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/beijing"];
                    [formatter setTimeZone:timeZone];
                    NSString *loctime = [formatter stringFromDate:dates];


(NSDate *) stringToDate:string
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:string];
    [dateFormatter release];
    return date;
}

-(NSInteger)daysWithinEraFromDate:(NSDate *) startDate toDate:(NSDate *) endDate
{
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    unsigned int unitFlags = NSDayCalendarUnit;
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:startDate  toDate:endDate  options:0];
    int days = [comps day];
    return days;
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多