我希望以下格式显示UIDatePicker中的选定日期。这怎么可能?请帮我
格式为:Sat Feb 23 3:42 PM
答案 0 :(得分:2)
以下是您希望输出的代码:
NSDate *chosen = [datePicker date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEE MMM dd hh:mm a"];
NSString *formated_date = [formatter stringFromDate:chosen];
NSLog(@"formated date is : %@", formated_date];
您可以通过阅读以下链接更改日期格式: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
答案 1 :(得分:1)
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ;
NSDateComponents *components = [gregorian components:unitFlags fromDate:yourPickerDate];
NSInteger yourWeekDay = [components weekday];
我认为这会对你有所帮助。
答案 2 :(得分:1)
NSDateFomatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormat:@"DDD MMM dd mm:ss aa"];
NSString *dateString = [dateFormatter stringFromDate:date];
答案 3 :(得分:0)
我明白了......
NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@“EEE MMM dd hh:mm aa”];
NSLog(@“格式为:%@”,[dateFormat stringFromDate:datePicker.date]);