常用时间戳转换记录
1. 将某个时间Str转化成 时间戳
#pragma mark - 将某个时间Str转化成 时间戳
+ (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. 将某个时间戳转化成 时间Str
#pragma mark - 将某个时间戳转化成 时间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"; //默认返回
}
return confromTimespStr;
}
网友评论