Question

I am using following code to get a date from string but its not showing correct date

NSDateFormatter *saveddf = [[NSDateFormatter alloc] init];

[saveddf setDateFormat:@"EEEE_MMMM dd_YYYY hh:mm a"];
[saveddf setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *savedtdate = [[NSDate alloc] init];
savedtdate = [saveddf dateFromString:datestring];



Saturday_June 22_2013 11:44 AM  -- INPUT (datestring).

2013-01-05 06:14:00 +0000 This is output i am getting.(savedtdate).

Please tell whats wrong in my code.

Was it helpful?

Solution

Try this code, it should work:

NSString *yourStringDate = @"Saturday_June 22_2013 11:44 AM";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"EEE_MMM dd_yyyy hh:mm a"];

NSDate *date = [dateFormatter dateFromString: yourStringDate];

  //Set the required date format
[dateFormatter setDateFormat:@"dd-MM-yyyy hh:mm"];

NSString *convertedString = [dateFormatter stringFromDate:date];
NSLog(@"Converted String : %@",convertedString);

OTHER TIPS

Try to use this code

NSDateFormatter *formatter;
NSString        *dateString;

formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy HH:mm"];
dateString = [formatter stringFromDate:[NSDate date]];
NSLog(@"%@",dateString);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top