作品链接:http://www.jianshu.com/users/1e0f5e6f73f6/top_articles
1.NSString *转 NSDate *
方法1.中国时间格式
// 时间字符串
NSString *string = @"2016-7-16 09:33:22";
// 日期格式化类
NSDateFormatter *format = [[NSDateFormatter alloc] init];
// 设置日期格式 为了转换成功
format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
// NSString * -> NSDate *
NSDate *data = [format dateFromString:string];
NSString *newString = [format stringFromDate:data];
方法2.混杂格式
// 时间字符串
NSString *string = @"7月-16/2016 09:33:22秒";
// 日期格式化类
NSDateFormatter *format = [[NSDateFormatter alloc] init];
// 设置日期格式 为了转换成功
format.dateFormat = @"MM月-dd-yyyy HH:mm:ss秒";
NSLog(@"%@",[format dateFromString:string]);
方法3.国际时间格式
// 时间字符串
NSString *string = @"Tue May 31 17:46:55 +0800 2016 ";
// 日期格式化类
NSDateFormatter *format = [[NSDateFormatter alloc] init];
// 设置日期格式 为了转换成功
format.dateFormat = @"EEE-MMM-dd HH:mm:ss Z yyyy";
//设置语言区域(因为这种时间是欧美常用时间)
format.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
方法4.时间戳格式
// 时间戳 : 从1970年1月1号 00:00:00开始走过的毫秒数
NSString *string = @"1287879863267";
// 获得有多少秒
NSTimeInterval second = string.longLongValue / 1000.0;
// 时间戳->NSDate
NSDate *date = [NSDate dateWithTimeIntervalSince1970:second];
1.NSDate *转 NSString *
方法1.datatostring
NSDate *date = [NSDate date];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = @"yyyy年MM月dd号 HH:mm:ss";
NSString *string = [format stringFromDate:date];
方法1.datatostring方法2.获取元素
NSString *string = @"016-07-16 21:16:26";
NSString *month = [string substringWithRange:NSMakeRange(5, 2)];
方法2.获取元素方法3.利用NSCalendar处理日期获取元素
NSString *string = @"2016-7-16 09:33:22";
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *data = [format dateFromString:string];
//利用NSCalendar处理日期
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger mouth = [calendar component:NSCalendarUnitMonth fromDate:data];
NSInteger hour = [calendar component:NSCalendarUnitHour fromDate:data];
NSInteger minute = [calendar component:NSCalendarUnitMinute fromDate:data];
NSInteger second = [calendar component:NSCalendarUnitSecond fromDate:data];
方法3.利用NSCalendar处理日期获取元素方法4 获取多位元素
NSString *string = @"2016-7-16 09:33:22";
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *date = [format dateFromString:string];
//利用NSCalendar处理日期
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit unit = NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitSecond|NSCalendarUnitMinute;
NSDateComponents *components = [calendar components:unit fromDate:date];
NSLog(@"%@",components);
方法4 获取多位元素3.时间比较
方法1.时间比较
// 时间字符串
NSString *createdAtString = @"2015-11-20 11:10:05";
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *createdAtDate = [format dateFromString:createdAtString];
// 手机当前时间
NSDate *nowDate = [NSDate date];
/**
NSComparisonResult的取值
NSOrderedAscending = -1L, // 升序, 越往右边越大
NSOrderedSame, // 相等
NSOrderedDescending // 降序, 越往右边越小
*/
// 获得比较结果(谁大谁小)
NSComparisonResult result = [nowDate compare:createdAtDate];
if (result == NSOrderedAscending) { // 升序, 越往右边越大
NSLog(@"createdAtDate > nowDate");
} else if (result == NSOrderedDescending) { // 降序, 越往右边越小
NSLog(@"createdAtDate < nowDate");
} else {
NSLog(@"createdAtDate == nowDate");
}
方法1.时间比较方法2.时间比较时间相差多少
// 时间字符串
NSString *createdAtString = @"2016-7-16 09:33:22";
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *createdAtDate = [format dateFromString:createdAtString];
// 手机当前时间
//NSDate *nowDate = [NSDate date];
// 获得createdAtDate和nowDate的时间间隔(间隔多少秒)
//NSTimeInterval interval = [nowDate timeIntervalSinceDate:createdAtDate];
NSTimeInterval interval = [createdAtDate timeIntervalSinceNow];
时间比较时间相差多少方法3.时间比较两个时间比较
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *createdAtString = @"2016-7-16 09:33:22";
NSDate *createdAtDate = [format dateFromString:createdAtString];
// 其他时间
NSString *otherString =@"2016-7-16 21:53:22";
NSDate *otherDate = [format dateFromString:otherString];
// 获得NSCalendar
NSCalendar *calendar = nil;
if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {
calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
}else{
calendar = [NSCalendar currentCalendar];
}
// 获得日期之间的间隔
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *components = [calendar components:unit fromDate:createdAtDate toDate:otherDate options:0];
NSLog(@"%@",components);
时间比较两个时间比较方法4.与当前时间比较是否为同一天
if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {
calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
}else{
calendar = [NSCalendar currentCalendar];
}
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSLog(@"%zd",[calendar isDateInToday:[format dateFromString:@"2016-7-16 09:33:22"]]);
方法4.与当前时间比较是否为同一天
网友评论