我想更改附加图像的格式
从YYYY / MM / DD到dd / MMM hh:min a但不幸的是我不能那样做
这是我在视图控制器3中的工作
#import "ViewController3.h"
@interface ViewController3 ()
@end
@implementation ViewController3
@synthesize text;
- (void)viewDidLoad {
[super viewDidLoad];
datePicker=[[UIDatePicker alloc]init];
datePicker.datePickerMode=UIDatePickerModeDate;
[self.text setInputView:datePicker];
UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[toolBar setTintColor:[UIColor grayColor]];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(ShowSelectedDate)];
UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolBar setItems:[NSArray arrayWithObjects:space,doneBtn, nil]];
[self.text setInputAccessoryView:toolBar];
}
-(void)ShowSelectedDate
{ NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"dd/MMM hh:min a"];
self.text.text=[NSString stringWithFormat:@"%@",[formatter stringFromDate:datePicker.date]];
[self.text resignFirstResponder];
}
@end
你还有其他想法吗?
答案 0 :(得分:0)
您希望以原始日期格式设置日期格式化程序,然后将结果转换为新的日期格式。
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY/MM/DD"];
NSString *originalDateString = [dateFormatter stringFromDate:datePicker.date];
[dateFormatter setDateFormat:@"dd/MMM hh:mm a"];
NSDate *newDate = [dateFormatter dateFromString:origianalDateString];
self.text.text = [NSString stringWithFormat:@"%@", [dateFormatter stringFromDate:newDate]];