1

hi can any on help me in achiving this is the string which i have 31 May 2016 04:30 PM(NSSTring) 2016-05-31T16:30:00.000+05:30(Required Format)

NSString *dateString = dateandtime; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 

[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"]; 
NSDate *dateFromString = [[NSDate alloc] init]; 
dateFromString = [dateFormatter dateFromString:dateString];

used the following code but returns nil

kiran kumar
  • 1,402
  • 17
  • 34
  • If you need to change the date format of a string, first you need to set your formatter to the string's format and create a date object with it, then change the date format to the required one and get a string out of it. – Desdenova May 27 '16 at 08:49
  • Try this http://stackoverflow.com/questions/2311421/converting-a-string-to-an-nsdate – srinivas n May 27 '16 at 09:06
  • see MY ANSWER THERE http://stackoverflow.com/questions/37252296/remove-milliseconds-from-string-in-ios/37252549#37252549 – Prashant Tukadiya May 27 '16 at 09:08

4 Answers4

1

The current format string does not contain any time zone information, so you have to set the time zone in the date formatter as well as the locale to be able to parse the string independently from the current locale.

The input format is dd MMM yyyy hh:mm a

 NSString *dateString = @"31 May 2016 04:30 PM";
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
 dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:19800];
 dateFormatter.dateFormat = @"dd MMM yyyy hh:mm a";
 NSDate *dateFromString = [dateFormatter dateFromString:dateString];
 dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSZ";
 NSString *stringFromDate =  [dateFormatter stringFromDate:dateFromString];
vadian
  • 274,689
  • 30
  • 353
  • 361
0

Some how I tried and worked

    NSString *myString = dateandtime;
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"dd MMM yyyy hh:mm a";
    NSDate *yourDate = [dateFormatter dateFromString:myString];
    dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ";
    NSLog(@"%@",[dateFormatter stringFromDate:yourDate]);
kiran kumar
  • 1,402
  • 17
  • 34
0

Whatever you do, test that your code works if the user doesn't use an Indian timezone, and if the user uses 24 hour time instead of AM/PM on their phone. I assume your "dateandtime" was created by converting an NSDate to an NSString; it would be much better to not do that but start with the original NSDate.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
-1
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];

NSString *currentDateString = @"2016-05-31T16:30:00.000+05:30";
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];        
NSDate *currentDate = [dateFormatter dateFromString:currentDateString];

NSLog(@"CurrentDate:%@", currentDate);

you can use this code.

Saurabh Jain
  • 1,688
  • 14
  • 28