ios NSDate NSTring long 時間戳與字符串轉換

  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);  
發佈了27 篇原創文章 · 獲贊 5 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章