1. 时间转化成时间戳
_timeStamp = [NSString stringWithFormat:@"%ld",[self timeSwitchTimestamp:_rightStr7 andFormatter:@"YYYY年MM月dd日"]];(@"YYYY年MM月dd日"可根据你设置的格式设置)
-(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与HH的区别:分别表示12小时制,24小时制
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
NSDate* date = [formatter dateFromString:formatTime]; //------------将字符串按formatter转成nsdate
//时间转时间戳的方法:
NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
NSLog(@"将某个时间转化成 时间戳&&&&&&&timeSp:%ld",(long)timeSp); //时间戳的值
return timeSp;
}
2. 时间戳转换为时间
NSString*str=@"1368082020";//时间戳
NSTimeIntervaltime=[strdoubleValue]+28800;//因为时差问题要加8小时 == 28800 sec
NSDate*detaildate=[NSDatedateWithTimeIntervalSince1970:time];
NSLog(@"date:%@",[detaildatedescription]);
//实例化一个NSDateFormatter对象
NSDateFormatter*dateFormatter = [[NSDateFormatteralloc]init];
//设定时间格式,这里可以设置成自己需要的格式
[dateFormattersetDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString*currentDateStr = [dateFormatterstringFromDate:detaildate];
网友评论