常用時間戳轉(zhuǎn)換記錄
1. 將某個時間Str轉(zhuǎn)化成 時間戳
#pragma mark - 將某個時間Str轉(zhuǎn)化成 時間戳
+ (NSInteger )timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:format]; //(@"YYYY-MM-dd hh:mm:ss")----------注意>hh為12小時制,HH為24小時制
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
NSDate* date = [formatter dateFromString:formatTime];
NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
return timeSp;
}
2. 將某個時間戳轉(zhuǎn)化成 時間Str
#pragma mark - 將某個時間戳轉(zhuǎn)化成 時間Str
+ (NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:format];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
if ([format isEqual:@"HH:mm:ss"] && confromTimespStr.length<8) {
confromTimespStr = @"00:00:00";
}
if ([format isEqual:@"YYYY-MM-dd HH:mm:ss"] && confromTimespStr.length<18) {
confromTimespStr = @"2000-01-01 00:00:00"; //默認(rèn)返回
}
return confromTimespStr;
}