NSDate,NSString,int 数据类型相互转换

来源:互联网 发布:陪吃陪喝陪玩的软件 编辑:程序博客网 时间:2024/06/04 19:28

NSString 类型数据转换成 NSDate 

- (NSDate *)dateFromStringg:(NSString *)dateString{    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];    [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];    NSDate *destDate= [dateFormatter dateFromString:dateString];    return destDate;}

NSLog(@"--日期字符串- %@", responseInJson[@"scantime"]);NSDate *Date = [self  dateFromStringg:responseInJson[@"scantime"]];NSLog(@"--日期Date --- %@", [NSString stringWithFormat:@"%@",Date]);

输出如下:



NSDate 转 int 类型

NSTimeInterval timeInterval = [Date timeIntervalSince1970];NSString *timeString = [NSString stringWithFormat:@"%d",(int)timeInterval];record.m_time = (int)timeInterval;NSLog(@"Date转整型-----%@", timeString);

输出如下:



int 类型转换成日期类型

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];NSDate* date = [NSDate dateWithTimeIntervalSince1970:self.record.m_time];<img src="http://img.blog.csdn.net/20151230185327635?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />NSString* startTime = [dateFormatter stringFromDate:date];


0 0