iOS标准时间与时间戳相互转换

 
 
一,转化的方法为
NSString *timeSp = [NSString stringWithFormat:@"%d", (long)[localeDate timeIntervalSince1970]];
NSLog(@"timeSp:%@",timeSp); //时间戳的值
二,把获取的时间转化为当前时间
NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate:datenow];
NSDate *localeDate = [datenow dateByAddingTimeInterval: interval];
NSLog(@"%@", localeDate);
3.把时间戳转化为时间的方法
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:1363948516];
NSLog(@"1363948516 = %@",confromTimesp);
//timer
NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate:datenow];
NSDate *localeDate = [datenow dateByAddingTimeInterval: interval];
NSLog(@"%@", localeDate);
NSString *timeSp = [NSString stringWithFormat:@"%lld", (long long)[localeDate timeIntervalSince1970]];
NSLog(@"timeSp:%@",timeSp); //时间戳的值 1369189763711 1369218563 1369218614
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:1369189763711/1000];
NSLog(@"1363948516 = %@",confromTimesp);
//实例化一个NSDateFormatter对象
//判断昨天 前几天等 判断今天凌晨时间戳
NSDateFormatter *dateFormatter1 = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter1 setDateFormat:@"yyyy-MM-dd 00:00:00"];
NSString *currentDateStr1 = [dateFormatter1 stringFromDate:[NSDate date]];
NSLog(@"凌晨时间:%@",currentDateStr1);
NSString *timeSp1 = [NSString stringWithFormat:@"%lld", (long long)[localeDate timeIntervalSince1970]];
NSLog(@"凌晨时间戳:%@",timeSp1);
//昨天凌晨时间戳
NSString *timeSp2 = [NSString stringWithFormat:@"%lld", (long long)[localeDate timeIntervalSince1970]-24*60*60];
NSLog(@"昨天凌晨时间戳:%@",timeSp2);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值