iOS日期的简单应用

字符串转化为日期:

NSString *strDate = @"2015-03-20 12:45:54";    
NSDateFormatter *frm = [[NSDateFormatter alloc] init];    
frm.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *date = [frm dateFromString:strDate];

国际时间转化:

NSString *str = @"Tue May 21 17:40:55 +0800 2014";    
NSDateFormatter *frm = [[NSDateFormatter alloc] init];    
frm.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";    
frm.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSDate *date = [frm dateFromString:str];

将现在时间转化为字符串:

NSDate *date = [[NSDate alloc] init];   
NSDateFormatter *frm = [[NSDateFormatter alloc] init];    
frm.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *str = [frm stringFromDate:date];

获取日期中的某个时间属性:

NSString *string = @"2014-01-20 09:10:05";    
NSDateFormatter *frm = [[NSDateFormatter alloc] init];    
frm.dateFormat = @"yyyy-MM-dd HH:mm:ss";    
NSDate *date = [frm dateFromString:string];       
NSCalendar *cld = [NSCalendar currentCalendar];    
NSInteger month = [cld component:NSCalendarUnitMonth fromDate:date];    
NSInteger hour = [cld component:NSCalendarUnitHour fromDate:date];
NSInteger day = [cld component:NSCalendarUnitDay fromDate:date];
NSString *string = @"2014-01-20 09:10:05";    
NSDateFormatter *frm = [[NSDateFormatter alloc] init];    
frm.dateFormat = @"yyyy-MM-dd HH:mm:ss";    
NSDate *date = [frm dateFromString:string];    
NSCalendar *cld = [NSCalendar currentCalendar];    
NSCalendarUnit unit = NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
NSDateComponents *dateCom = [cld components:unit fromDate:date];

和手机现在时间比较:

NSString *string = @"2019-01-20 09:10:05";    
NSDateFormatter *frm = [[NSDateFormatter alloc] init];    
frm.dateFormat = @"yyyy-MM-dd HH:mm:ss";    
NSDate *creatdate = [frm dateFromString:string];    
NSDate *nowDate = [NSDate date];    
NSComparisonResult result = [nowDate compare:creatdate];    
/*     
NSOrderedAscending = -1L, 升序     
NSOrderedSame,     
NSOrderedDescending 降序     
*/   
if (result == NSOrderedAscending) {        
   NSLog(@"未来时间");    
}else if (result == NSOrderedSame){          
   NSLog(@"现在时间");    
}else if (result == NSOrderedDescending){         
   NSLog(@"过去时间");
}

距离现在多少秒:

NSString *string = @"2015-11-21 19:38:05";   
NSDateFormatter *frm = [[NSDateFormatter alloc] init];    
frm.dateFormat = @"yyyy-MM-dd HH:mm:ss";    
NSDate *creatdate = [frm dateFromString:string];
NSTimeInterval interval = [creatdate timeIntervalSinceNow];

两个时间比较:

NSDateFormatter *frm = [[NSDateFormatter alloc] init];    
frm.dateFormat = @"yyyy-MM-dd HH:mm:ss";    
NSString *createdAtString = @"2015-11-01 09:10:05";    
NSDate *createdAtDate = [frm dateFromString:createdAtString];       
NSString *otherString = @"2015-10-31 08:56:45";    
NSDate *otherDate = [frm dateFromString:otherString];       
NSCalendar *calendar = nil;    
// iOS8.0之后使用    
if ([NSCalendar resolveInstanceMethod:@selector(calendarWithIdentifier:)]) { 
       calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian ];    
}else{           
       calendar = [NSCalendar currentCalendar];    
}    
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;    
NSDateComponents *com = [calendar components:unit fromDate:createdAtDate toDate:otherDate options:0];
NSLog(@"%@",com);

判断是否是今天:

NSCalendar *calendar = nil;   
if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {  
      calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];   
 } else {   
     calendar = [NSCalendar currentCalendar];  
  }     
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];    
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";   
NSLog(@"%zd", [calendar isDateInToday:[fmt dateFromString:@"2015-11-10 01:09:56"]]);
    // 是否是昨天
    - (BOOL)isDateInYesterday:(NSDate *)date 
    // 是否是明天
    - (BOOL)isDateInTomorrow:(NSDate *)date
    // 是否在一个星期之内
    - (BOOL)isDateInWeekend:(NSDate *)date
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
    大学一百阅读 3,270评论 0 4
  • 你的难可以告诉谁? 难即难过、难处、困难、伤心、无奈、无助…… 它们是负面情绪,是人们常说的负能...
    黄希儿阅读 299评论 7 6
  • 我们都喜欢在云霞收练之前遐想 然后期待恒久的星光 可是当第二天的朝露一来 除了漫身的咸香 对岁月又别无他方
    我是秀啊阅读 349评论 3 4
  • 这种感觉真的很糟 因为一句话就高兴半天 发现了一件事就心凉半截 喜欢不能太明显 不然不值钱.
    冷漠无情的犬哥阅读 146评论 0 1
  • 首饰叠戴这个风潮从去年就开始慢慢吹向时尚界,2016年的春天,这股风没有要消退的态势,反而愈演愈烈。一条项链,一款...
    茉客阅读 1,407评论 0 0