随笔-OC获取系统时间,获取绝对时间,获得真实时间

  1. //获得系统时间
  2. NSDate * senddate=[NSDate date];
  3. NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
  4. [dateformatter setDateFormat:@"HH:mm"];
  5. NSString * locationString=[dateformatter stringFromDate:senddate];
  6. //[dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];
  7. //NSString * morelocationString=[dateformatter stringFromDate:senddate];

  8. //获得系统日期
  9. NSCalendar * cal=[NSCalendar currentCalendar];
  10. NSUInteger unitFlags=NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit;
  11. NSDateComponents * conponent= [cal components:unitFlags fromDate:senddate];
  12. NSInteger year=[conponent year];
  13. NSInteger month=[conponent month];
  14. NSInteger day=[conponent day];
  15. NSString * nsDateString= [NSString stringWithFormat:@"M年-月-日",year,month,day];

  16. [dateformatter release];

2. 从字符串来获得NSDate
  1. string strYear="1988";
  2. string strMonth="09";
  3. string strDay="18";
  4. string strHour="5";
  5. string strMinutes="18";
  6. string strSec="20";



  7. morelocationString=[NSString stringWithFormat:@"%s-%s-%s-%s-%s-%s",strYear.c_str(),strMonth.c_str(),
  8. strDay.c_str(),strHour.c_str(),strMinutes.c_str(),strSec.c_str()];


  9. //根据时间字符串获得NSDate
  10. NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
  11. [dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];
  12. NSDate * oldDate=[dateformatter dateFromString:morelocationString];

3. 从GMT时间,得到本地时间
  1. NSDate * oldDate=[dateformatter dateFromString:morelocationString];


  2. NSTimeInterval timeZoneOffset=[[NSTimeZone systemTimeZone] secondsFromGMT];
  3. NSDate * newDate=[oldDate dateByAddingTimeInterval:timeZoneOffset];

你可能感兴趣的:(ios,NSData,本地时间)