22A335A0-7527-4858-A393-1766E5A5419C.png
B144D19F-48B0-42E2-A28C-5A16BB8B3F21.png
CE87CCC6-1868-4DC1-A445-F327DCE3D706.png
//
// RootViewController.m
// UIDataPicker
//
// Created by 林杰 on 16/8/2.
// Copyright © 2016年 TenMios. All rights reserved.
//
#import "RootViewController.h"
@interface RootViewController ()
@end
#define TheDatePickerTag 1002
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIDatePicker *datePicker =[[UIDatePicker alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 200)];
datePicker.tag = TheDatePickerTag;
//最小日期
datePicker.minimumDate = [NSDate dateWithTimeIntervalSinceNow:-60*60*24*365*30];
datePicker.date = [NSDate date];
//最大日期
datePicker.maximumDate = [NSDate dateWithTimeIntervalSinceNow:60*60*24*365*50];
// 设置显示的模式
datePicker.datePickerMode = UIDatePickerModeDate;
[self.view addSubview: datePicker];
}
//确定按钮点击
- (IBAction)buttonClick:(id)sender {
UIDatePicker *datePicker= (UIDatePicker *)[self.view viewWithTag:TheDatePickerTag];
NSLog(@"%@",datePicker.date);
// NSDate -> NSString
NSDateFormatter *formatter =[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd"]; //也可以用 yyyy/MM/dd
NSString *stringDate = [formatter stringFromDate:datePicker.date];
NSLog(@"%@",stringDate);
}
@end
网友评论