Posted By

sag333ar on 11/18/10


Tagged

iphone objectivec NSDate iPad iOS4


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

sag333ar


Grabing Date from iOS4/iPad/iPhone without GMT format problem


 / Published in: Objective C
 

  1. NSString *strDate;
  2.  
  3.  
  4. NSDate *d=[NSDate date];
  5.  
  6. NSString *strDateGMT=[d description];
  7.  
  8. //NSLog(@"%@",strDateGMT);
  9.  
  10. NSRange range = [strDateGMT rangeOfString:@"GMT"];
  11. if (range.location != NSNotFound)
  12. {
  13. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  14. [dateFormatter setDateStyle:kCFDateFormatterLongStyle];
  15. [dateFormatter setTimeStyle:kCFDateFormatterLongStyle];
  16. [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
  17. NSString *dateGMTString = [dateFormatter stringFromDate:d];
  18. [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
  19. NSString *dateLocalString = [dateFormatter stringFromDate:d];
  20. [dateFormatter release];
  21. // NSLog(@"%@",dateGMTString);
  22. // NSLog(@"%@",dateLocalString);
  23.  
  24. // %@ - 1 - For Date
  25. // %@ - 2 - For Month
  26. // %@ - 3 - For Year
  27. // %@ - 4 - For Time
  28. // %@ - 5 - For AM/PM
  29.  
  30. NSArray *ar=[dateLocalString componentsSeparatedByString:@" "];
  31.  
  32. if([ar containsObject:@"AM"] || [ar containsObject:@"PM"] ) {
  33. // if iPhone is Set without 24Hours timing Mode
  34. strDate =[NSString stringWithFormat:@"%@ %@ %@ %@ %@",[ar objectAtIndex:0],[ar objectAtIndex:1],[ar objectAtIndex:2],[ar objectAtIndex:3],[ar objectAtIndex:4]];
  35. } else {
  36. // if iPhone is Set with 24Hours timing Mode
  37. strDate =[NSString stringWithFormat:@"%@ %@ %@ %@",[ar objectAtIndex:0],[ar objectAtIndex:1],[ar objectAtIndex:2],[ar objectAtIndex:3]];
  38. }
  39. } else {
  40. NSArray *ar=[[[NSDate date] description] componentsSeparatedByString:@" "];
  41. strDate=[NSString stringWithFormat:@"%@ %@",[ar objectAtIndex:0],[ar objectAtIndex:1]];
  42. }

Report this snippet  

You need to login to post a comment.