1.字符串转13位时间戳
- (NSString*)getTimeStrWithString:(NSString*)str{
NSDateFormatter *dateFormatter = [[NSDate Formatter alloc] init];// 创建一个时间格式化对象
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//设定时间的格式
NSDate*tempDate = [dateFormatter dateFromString:str];//将字符串转换为时间对象
NSString *timeStr = [NSString stringWithFormat:@"%ld", (long)[tempDate
timeIntervalSince1970]*1000];//字符串转成时间戳,精确到毫秒*1000
return timeStr;
}
2. 13位时间戳转字符串
- (NSString*)timestampToString:(NSString*)timestamp{
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[timestamp doubleValue]/1000];
NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString* string=[dateFormat stringFromDate:confromTimesp];
return string;
}
3. 判断字符串是不是纯数字
//过滤掉数字 判断剩下的如果长度>0 就不是纯数字
- (BOOL)isNum:(NSString*)checkedNumString {
checkedNumString = [checkedNumString stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
if(checkedNumString.length>0) {
returnNO;
}
return YES;
}