//日期转换为时间戳 (日期转换为秒数)
NSDate *date = [NSDate date]; NSLog(@"当前日期为(0时区):%@",date); NSDateFormatter *dateFormetter = [[NSDateFormatter alloc] init]; [dateFormetter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *timeStr = [dateFormetter stringFromDate:date]; NSLog(@"timeStr(当前时区) = %@",timeStr); NSTimeInterval timeStamp= [date timeIntervalSince1970]; NSLog(@"日期转换为时间戳 %@ = %f", date, timeStamp);
//时间戳转日期 (秒数转日期)
NSString *timeStamp2 = @"1449732239"; long long int date1 = (long long int)[timeStamp2 intValue]; NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:date1]; NSLog(@"时间戳转日期 %@ = (0时区)%@", timeStamp2, date2); NSDateFormatter *dateFormetter1 = [[NSDateFormatter alloc] init]; [dateFormetter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *timeStr1 = [dateFormetter1 stringFromDate:date2]; NSLog(@"当前时区timeStr = (8时区当前时区)%@", timeStr1);