您的位置:首页 > 移动开发 > IOS开发

iOS开发之时间与时间戳的转换

2015-05-09 18:44 344 查看
// 获取当前系统的时间
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
// 将时间按照formater的格式转换为字符串
NSString *currentTime = [formater stringFromDate:[NSDate date]];
NSLog(@"%@", currentTime);

// 将当前时间转为时间戳
NSString *timeS = [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]];
// 打印时间戳
NSLog(@"%@", timeS);

// 将时间戳转时间的方法
NSInteger time = [timeS integerValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
NSString *dateStr = [formater stringFromDate:date];
NSLog(@"%@", dateStr);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: