iOS 时间字符串&NSDate&时间戳 相互转
2017-05-17 本文已影响4556人
叶落似晚秋
- 前段时间一直在和时间打交道,这几天整理了一下几个时间相互转换的方法,再次做记录
- 效果图
- 时间戳转字符串
+(NSString *)timeStampConversionNSString:(NSString *)timeStamp
{
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timeStamp longLongValue]/1000];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateStr = [formatter stringFromDate:date];
return dateStr;
}
- 时间转时间戳
+(NSString *)dateConversionTimeStamp:(NSDate *)date
{
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]*1000];
return timeSp;
}
- 字符串转时间
+(NSDate *)nsstringConversionNSDate:(NSString *)dateStr
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSDate *datestr = [dateFormatter dateFromString:dateStr];
return datestr;
}