iOS 时间类常用方法

    //当前日前日期

    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];



   

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。