打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

- (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;
}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
iOS 疯狂基础之NSDate 时间
时区日期处理及定时 (NSDate,NSCalendar,NSTimer,NSTimeZone)
Objective
iOS标准时间与时间戳相互转换
NSDate NSCalendar(日历立法) NSDateComponents(日期组件)的一些用法
iOS常用公共方法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服