Reputation: 11
Input: Fri, 26 Apr 2013 21:56:31 EDT
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
[df1 setDateFormat:@"EE, dd MMM YYYY HH:mm:ss zzz"];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"];
NSString *startDateStr2 = [df1 stringFromDate:[NSDate date]];
NSLog(@"currentDate: %@", startDateStr2);
but i am getting output : Sat, 05 Jan 2013 07:26:31 GMT+05:30
Upvotes: 0
Views: 98
Reputation: 20541
Use below method which is used to change the format of the NSDate
..
-(NSString *)changeDateFormat:(NSString*)stringDate dateFormat:(NSString*)dateFormat getwithFormat:(NSString *)getwithFormat{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:dateFormat];
NSDate *date = [dateFormatter dateFromString:stringDate];
dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:getwithFormat];
NSString *convertedString = [dateFormatter stringFromDate:date];
NSLog(@"Converted String : %@",convertedString);
return convertedString;
}
Call above method with its 3 parameters..
1. stringDate : Pass your date which in string type.
2. dateFormat : set format which is used in your passed string date. For example, if your string date is "14-03-2013 11:22 am"
then set the format to @"dd-MM-yyyy hh:mm a"
.
3. getwithFormat : Set format which you want, as if you want date 14/03/2013
then just set format @"dd/MM/yyyy"
.
How to call this see below example with method:
NSString *strSDate = [self changeDateFormat:@"14-03-2013 11:22 am" dateFormat:@"dd-MM-yyyy hh:mm a" getwithFormat:@"dd/MM/yyyy"];
NSLog(@"\n\n Now Date => %@",strSDate);
Output is : Now Date => 14/03/2013
hope its helpful to you...
Upvotes: 2