NSDate 时间戳与字符串转换

  1. 一,转化的方法为  
  2.     NSString *timeSp = [NSString stringWithFormat:@"%d", (long)[localeDate timeIntervalSince1970]];  
  3.     NSLog(@"timeSp:%@",timeSp); //时间戳的值  
  4.   
  5. 二,把获取的时间转化为当前时间  
  6.  NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式  
  7.     NSTimeZone *zone = [NSTimeZone systemTimeZone];  
  8.     NSInteger interval = [zone secondsFromGMTForDate:datenow];  
  9.     NSDate *localeDate = [datenow  dateByAddingTimeInterval: interval];  
  10.     NSLog(@"%@", localeDate);  
  11.   
  12. 3.把时间戳转化为时间的方法  
  13.     NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:1363948516];  
  14.     NSLog(@"1363948516  = %@",confromTimesp);  
  15.   
  16. //timer  
  17.     NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式  
  18.     NSTimeZone *zone = [NSTimeZone systemTimeZone];  
  19.     NSInteger interval = [zone secondsFromGMTForDate:datenow];  
  20.     NSDate *localeDate = [datenow  dateByAddingTimeInterval: interval];  
  21.     NSLog(@"%@", localeDate);  
  22.       
  23.     NSString *timeSp = [NSString stringWithFormat:@"%lld", (long long)[localeDate timeIntervalSince1970]];  
  24.     NSLog(@"timeSp:%@",timeSp); //时间戳的值 1369189763711   1369218563 1369218614  
  25.       
  26.     NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:1369189763711/1000];  
  27.     NSLog(@"1363948516  = %@",confromTimesp);  
  28.       
  29.     //实例化一个NSDateFormatter对象  
  30.   
  31.     //判断昨天 前几天等 判断今天凌晨时间戳  
  32.     NSDateFormatter *dateFormatter1 = [[[NSDateFormatter alloc] init] autorelease];  
  33.     [dateFormatter1 setDateFormat:@"yyyy-MM-dd 00:00:00"];  
  34.     NSString *currentDateStr1 = [dateFormatter1 stringFromDate:[NSDate date]];  
  35.     NSLog(@"凌晨时间:%@",currentDateStr1);  
  36.     NSString *timeSp1 = [NSString stringWithFormat:@"%lld", (long long)[localeDate timeIntervalSince1970]];  
  37.     NSLog(@"凌晨时间戳:%@",timeSp1);  
  38.     //昨天凌晨时间戳  
  39.     NSString *timeSp2 = [NSString stringWithFormat:@"%lld", (long long)[localeDate timeIntervalSince1970]-24*60*60];  
  40.     NSLog(@"昨天凌晨时间戳:%@",timeSp2);  
原文地址:https://www.cnblogs.com/fanjing/p/4568467.html