2

I have a date string like this "2013-09-05T06:40:19Z" from server i want to convert it into date which format should be same as my iphone follow .

how to do this ? when i do this ?

NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];       
[dateFormatter1 setDateFormat:@"MMM dd yyyy"];       
[dateFormatter1 setLocale:[NSLocale currentLocale]];        
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];       
NSDate *date =[dateFormatter1 dateFromString:time];
NSLog(@"%@",date)//getting null here 

i am getting null in date.

IronManGill
  • 7,222
  • 2
  • 31
  • 52
sundeep
  • 43
  • 1
  • 8
  • `setDateFormat:` should match your string's format. Do it with the help of this. Pretty easy. http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns – Desdenova Sep 05 '13 at 07:48
  • see this link, http://stackoverflow.com/questions/14640929/convert-nsstring-to-nsdate – karthika Sep 05 '13 at 08:35

3 Answers3

2
NSString *dateStr = @"2013-09-05T06:40:19Z";
NSString *outDateFormate = @"MMM dd yyyy";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[NSLocale currentLocale]];
    [dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *intime = [dateFormat dateFromString:dateStr];
    [dateFormat  setDateFormat:outDateFormate];
    NSString *dateWithNewFormat = [dateFormat stringFromDate:intime];
Naveen kumar
  • 800
  • 1
  • 5
  • 23
0
NSString *str=@"2013-09-05T06:40:19Z";
str=[[str stringByReplacingOccurrencesOfString:@"T" withString:@" "]stringByReplacingOccurrencesOfString:@"Z" withString:@""];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
[dateFormatter1 setLocale:[NSLocale currentLocale]];
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *date =[dateFormatter1 dateFromString:str];
[dateFormatter1 setDateFormat:@"MMM dd yyyy"];
NSString *newDate=[dateFormatter1 stringFromDate:date];

NSLog(@"%@",newDate)
Developer
  • 760
  • 3
  • 15
  • `new` is a keyword for `alloc & init`. It's a pretty bad idea to use it as a variable name. – Desdenova Sep 05 '13 at 07:56
  • @Desdenova `new` is not a keyword in obj-c, just a `NSObject`'s method name. In obj-c++ it is, though. However, you are right, and this is a bad name for variable – medvedNick Sep 05 '13 at 08:01
  • @medvedNick Thanks for the clarification. Didn't know the details of it. – Desdenova Sep 05 '13 at 08:05
0

Since you are getting the date in specified format so you need to define your date formatter accordingly

 NSString *gameDateString = @"2013-09-05T06:40:19Z";

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];  
 [dateFormatter setLocale:[NSLocale currentLocale]];
 [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; 

 NSDate *gameDate = [dateFormatter dateFromString:gameDateString];
 NSLog@("** The date is %@",gameDate); 

   // Now convert date to string in the format which you required.
  [dateFormatter  setDateFormat:@"MMMM dd, yyyy"];
  NSString *dateString =  [dateFormatter stringFromDate:gameDate];
  NSLog@("** The date string is %@",dateString); 

Hope this helps.

Gyanendra Singh
  • 1,483
  • 12
  • 15
  • @sundeep Glad it helped. If it is not a too much work can you mark this as accepted answer? – Gyanendra Singh Sep 05 '13 at 10:00
  • @Gyanendersing i want a format like this dateFormatter setDateFormat:@"hh:mm aa Today"]; can i do this .how to do this – sundeep Sep 05 '13 at 12:17
  • I am afraid there is no such format available. Can you be more specific what you actually want? I mean to say how do you want the date string e.g. Sept 05, 2013 or 05/09/2013 or in any other format? – Gyanendra Singh Sep 05 '13 at 12:31