#import "ViewController.h"
@interface ViewController ()
@property (strong,nonatomic) UIDatePicker* dp;
@property (strong,nonatomic) UILabel* lb;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//dateTimePicker
self.dp = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 200, 400, 200)];
self.dp.datePickerMode = UIDatePickerModeDateAndTime;
self.dp.locale = [NSLocale localeWithLocaleIdentifier:@"zh-Hans"];
[self.view addSubview:self.dp];
//label
self.lb = [[UILabel alloc]initWithFrame:CGRectMake(100, 500, 200, 21)];
self.lb.text = @"label";
self.lb.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.lb];
//button
UIButton* b1 = [UIButton buttonWithType:UIButtonTypeSystem];
b1.frame = CGRectMake(200, 600, 100, 50);
[b1 setTitle:@"button" forState:UIControlStateNormal];
[b1 addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:b1];
}
-(void)onclick:(id)sender{
NSDate* date = self.dp.date;
NSLog(@"%@",[date descriptionWithLocale:[NSLocale currentLocale]]);
NSDateFormatter* df = [[NSDateFormatter alloc] init];
df.dateFormat = @"YYYY-MM-dd HH:mm:ss";
NSLog(@"%@",[df stringFromDate:date]);
self.lb.text = [df stringFromDate:date];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end