NSDate * date = [NSDate date];
// 獲取當前時間戳
NSString * ctime = [NSString stringWithFormat:@"%ld",(long)[date timeIntervalSince1970]];
NSLog(@"時間戳為:%@",ctime);
// 獲取當前時間
NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"YY-MM-dd HH:mm"];
NSString * time = [formatter stringFromDate:date];
NSLog(@"%@",time);
// 時間戳轉(zhuǎn)化成時間字符串
NSTimeInterval tempTime = [ctime longLongValue];
// 時間戳轉(zhuǎn)NSDate
NSDate * ctimeDate = [NSDate dateWithTimeIntervalSince1970:tempTime];
NSString * timeStr = [formatter stringFromDate:ctimeDate];
NSLog(@"轉(zhuǎn)化的時間:%@",timeStr);
// 字符串時間轉(zhuǎn)時間戳
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
NSString * time = [NSString stringWithFormat:@"%ld", (long)[[formatter dateFromString:@"2016-07-22 04:00"] timeIntervalSince1970]];
NSLog(@"%@",time);
記性不好的人,只能貼出來方便查看