I have string date such like,
NSString * dateStr = @"2014-12-31";
I want to get month in three letter month such like Des (from dateStr) and day such like 31
I tried about 2 hour but i can't get it properly..
My code is..
First convert string to date ;
NSString *dateStr = [[self.listOfEvents objectAtIndex:indexPath.row] objectForKey:@"EventDate"];
NSDateFormatter *DTFormatter = [[NSDateFormatter alloc] init];
[DTFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *eventDate = [DTFormatter dateFromString:dateStr];
NSLog(@"eventDate - %@", eventDate);
And now convert date to string;
NSDateFormatter *currentDTFormatter = [[NSDateFormatter alloc] init];
[currentDTFormatter setDateFormat:@"dd MMM"];
NSString *eventDateStr = [DTFormatter stringFromDate:eventDate];
NSLog(@"eventDateStr - %@", eventDateStr);
So, please suggest me where i was wrong here.
EDITED :
I got console output is
eventDate - 2014-12-31 18:30:00 +0000
eventDateStr - 2014-12-31
The problem is you are converting using old dateformatter that is DTFormatter
you have to use currentDTFormatter in place of it.
Try with currentDTFormatter
this formatter.
NSDateFormatter *currentDTFormatter = [[NSDateFormatter alloc] init];
[currentDTFormatter setDateFormat:@"dd MMM"];
NSString *eventDateStr = [currentDTFormatter stringFromDate:eventDate];
NSLog(@"%@", eventDateStr);
Try adding this line [currentDTFormatter setDateStyle:NSDateFormatterMediumStyle];
before you are calling setDateFormat
Here is the appropriate passage from apple's documentation:
NSDateFormatterMediumStyle
Specifies a medium style, typically with abbreviated text, such as “Nov 23, 1937”.
Equal to kCFDateFormatterMediumStyle.
Available in OS X v10.4 and later.
Declared in NSDateFormatter.h.
这也是在这里说https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1仅使用一个“ d”因此,“ EdMMM”应该适用于英语语言环境,或者我猜“ dMMM”甚至是“ d MMM”
Have you tried:
[formatter setDateFormat:@"dd-MMM-YYYY HH:mm"];
and then
[formatter stringFromDate:someNSDate];
Here is the Solution
NSString * dateStr = @"2014-12-31";
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd"];
NSDate* date = [df dateFromString:dateStr];
NSLog(@"%@", date);
[df setDateFormat:@"dd MMM"];
NSString *dateString = [df stringFromDate:date];
NSLog(@"%@", dateString);
Try out
Please try to use the below code to convert your date string...
- (NSString*) formatDateString:(NSString*)date
{
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *formattedDate = [inputFormatter dateFromString: date];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"dd MMM"];
NSString *displayDate = [NSString stringWithFormat:@"%@", [outputFormatter stringFromDate:formattedDate]];
return displayDate;
}
And also you can set format styles to NSDateFormatter
for formatting your date string
setTimeStyle:
setDateStyle:
You can check NSDateFormatter
for your reference from below link.
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.