iOS 时间类常用方法

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

//当前日前日期

    NSDate *today = [NSDate date];


    //时区

    NSTimeZone *zone = [NSTimeZone systemTimeZone];


    //设置间隔

    _interval = [zone secondsFromGMTForDate: today];


    //今天+8小时(中国上海时区)

    today = [today  dateByAddingTimeInterval: _interval];


    //获取明天日期

    NSDate *tomorrowDate = [[NSDate alloc]initWithTimeInterval:24 * 60 * 60 sinceDate:[NSDate date]];


    //日历 

   NSCalendar *calendar = [NSCalendar currentCalendar];


   //comps里可以分离出时间年月日等信息

NSDateComponents *comps = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit |NSWeekdayOrdinalCalendarUnit | NSHourCalendarUnit) fromDate:tomorrowDate];


   //获取当前星期几   

    NSInteger weekday = [comps weekday];


  //将NSString类型转换成NSDate    

    NSDate *serverDate = [dateFomatter dateFromString:serverTimeStr];

    NSDate *createDate = [dateFomatter dateFromString:createTimeStr];

 

    //算出两个Date的间隔

   NSTimeInterval betweenTime = [serverDate timeIntervalSinceDate:createDate];


   //分离出标准格式里的年月日时分秒  

    NSDateFormatter *dateFomatter = [[NSDateFormatter alloc]init];

    [dateFomatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];


    //将string换成formatter形式

    NSDate *stringDate = [dateFomatter dateFromString:string];


    //formatter为月和日

    [dateFomatter setDateFormat:@"MM-dd"];


    //提取字符串中月和日

    NSString *str1 = [dateFomatter stringFromDate:stringDate];