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

iOS开发——计算时间差,计算日期查,计算年龄。

时间:2015-11-17 12:42:39      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

  我开发的很多项目中都有用到计算时间差的地方,这里把我封装的一个小方法分享给需要的朋友,当然主要是初学者们,哈哈。

  主要使用了NSDate、NSCalender、NSComponents三个类,这三个类里面的方法很多,大家都可以浏览一下,特别需要注意方法后面的版本限制,注意使用不要出现版本适配问题。废话不多,直接Show you the code:

  首先是.h

 1 /**
 2  *  计算时间差
 3  *
 4  *  @param startDate 开始时间
 5  *  @param endDate   结束时间
 6  *
 7  *  @return 时间差
 8  */
 9 + (NSDateComponents *)calculateTimeDifferenceWithStartDateString:(NSString *)startString endDateString:(NSString *)endString;
10 + (NSDateComponents *)calculateTimeDifferenceWithStartDate:(NSDate *)startDate endDate:(NSDate *)endDate;
11 + (NSDateComponents *)calculateAgeWithDate:(NSDate *)date;
12 + (NSDateComponents *)calculateAgeWithDateString:(NSString *)string;

  然后是.m

 1 + (NSDateComponents *)calculateTimeDifferenceWithStartDateString:(NSString *)startString endDateString:(NSString *)endString {
 2     
 3     startString = [self dateStringFormatter:startString];
 4     endString = [self dateStringFormatter:endString];
 5     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 6     [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
 7     [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
 8     
 9     return [self calculateTimeDifferenceWithStartDate:[dateFormatter dateFromString:startString] endDate:[dateFormatter dateFromString:endString]];
10 }
11 
12 + (NSDateComponents *)calculateTimeDifferenceWithStartDate:(NSDate *)startDate endDate:(NSDate *)endDate {
13     
14     // 日历
15     NSCalendar *calendar = [NSCalendar currentCalendar];
16     // 日期对比项
17     NSUInteger unitFlags = NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday;
18 
19     return [calendar components:unitFlags fromDate:startDate toDate:endDate options:NSCalendarMatchStrictly];
20 }
21 
22 + (NSDateComponents *)calculateAgeWithDate:(NSDate *)date {
23     
24     return [self calculateTimeDifferenceWithStartDate:date endDate:[NSDate date]];
25 }
26 
27 + (NSDateComponents *)calculateAgeWithDateString:(NSString *)dateString {
28     
29     dateString = [self dateStringFormatter:dateString];
30     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
31     [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
32     [dateFormatter setTimeZone:[NSTimeZone localTimeZone]]; //设定日期时区
33     
34     return [self calculateAgeWithDate:[dateFormatter dateFromString:dateString]];
35 }
36 
37 /**
38  *  日期字符串格式化方法
39  *
40  *  @param dateString 日期字符串
41  *
42  *  @return 格式化后的字符串
43  */
44 + (NSString *)dateStringFormatter:(NSString *)dateString {
45     
46     // 去日期字符串的符号
47     dateString = [dateString stringByReplacingOccurrencesOfString:@"-" withString:@""];
48     dateString = [dateString stringByReplacingOccurrencesOfString:@":" withString:@""];
49     dateString = [dateString stringByReplacingOccurrencesOfString:@"/" withString:@""];
50     dateString = [dateString stringByReplacingOccurrencesOfString:@" " withString:@""];
51     
52     // 时间字符串长度超出,截取14位
53     if ([dateString length] > 14) {
54         dateString = [dateString substringToIndex:13];
55     }
56     
57     // 时间字符串长度不够,补0
58     while ([dateString length] < 14) {
59         dateString = [dateString stringByAppendingString:@"0"];
60     }
61     
62     return dateString;
63 }

  谢谢观看!

iOS开发——计算时间差,计算日期查,计算年龄。

标签:

原文地址:http://www.cnblogs.com/leotangcn/p/4971236.html

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