码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 时间类常用方法

时间:2015-03-20 07:01:46      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

    //当前日前日期

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



   

iOS 时间类常用方法

标签:

原文地址:http://blog.csdn.net/liuxu0718/article/details/44481325

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!