This question already has an answer here:
-
Convert string to date in my iPhone app
4 answers
-
issue with date formatting using NSDateFormatter
3 answers
How can i convert the following string "October 24,Monday 12:30 AM EST" to NSDateFormat.
i tried this code
NSDateFormatter *dateformat=[[NSDateFormatter alloc]init];
[dateformat setDateFormat:@"MM/DD/YYYY hh:mm a"];
NSDate *datefor=[dateformat dateFromString:appDelegate.appoinmentString];
[dateformat setDateFormat:@"MM-dd-yyyy hh:mm a"];
NSString *dateStr=[dateformat stringFromDate:datefor];
NSDate *datetype=[dateformat dateFromString:dateStr];
Your dateformat
for October 24,Monday 12:30 AM EST
is not correct. The correct dateformat
in your case is MMMM dd,eeee HH:mm a z
.
Working Code :
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:@"MMMM dd,eeee HH:mm a z"];
NSDate *myDate = [dateformat dateFromString:@"October 24,Monday 12:30 AM EST"];
Take a look at Date Format Specifiers.
eeee
- Local day of week spelled out.
MMMM
- Month spelled out.
dd
- day of month with no leading zeros.
HH
- hour of day (24 hour format).
mm
- minutes of hour (with leading zero) .
z
- timezone (short wall
time).
Try this...
NSString *p=@"October 24,Monday 12:30 AM EST";
NSDateFormatter *dateformat=[[NSDateFormatter alloc]init];
[dateformat setDateFormat:@"MMM dd,EEEE hh:mm a vvv"];
NSDate *datefor=[dateformat dateFromString:p];
[dateformat setDateFormat:@"MM-dd-yyyy hh:mm a"];
NSString *dateStr=[dateformat stringFromDate:datefor];
NSDate *datetype=[dateformat dateFromString:dateStr];
Let me know if you have any problem.
Try like below:-
NSString *dateStr=@"October 24,Monday 12:30 AM EST";
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"EST"]];
[dateFormat setDateFormat:@"MMMM dd,eeee HH:mm a z"];
NSDate *changeDate=[dateFormat dateFromString:dateStr];
NSLog(@"changeDate=%@",changeDate);
NSString *str=[dateFormat stringFromDate:changeDate];
//getting your local time
NSTimeZone *tz=[NSTimeZone localTimeZone];
//setting yourlocal time
[dateFormat setTimeZone:tz];
NSDate *date = [dateFormat dateFromString:str];
//Setting your desired format
[dateFormat setDateFormat:@"dd-mm Z"];
NSString *newDate=[dateFormat stringFromDate:date];
NSLog(@"new date=%@",newDate);