年月日,周几,分秒,代码
NSDate*date=[NSDatedate];
NSCalendar*calendar=[NSCalendarcurrentCalendar];
NSDateComponents*comps;
//年月日获得
comps=[calendarcomponents:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit)
fromDate:date];
NSIntegeryear=[compsyear];
NSIntegermonth=[compsmonth];
NSIntegerday=[compsday];
NSLog(@"year:%dmonth:%d,day:%d",year,month,day);
//当前的时分秒获得
comps=[calendarcomponents:(NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit)
fromDate:date];
NSIntegerhour=[compshour];
NSIntegerminute=[compsminute];
NSIntegersecond=[compssecond];
NSLog(@"hour:%dminute:%dsecond:%d",hour,minute,second);
//周几和星期几获得
comps=[calendarcomponents:(NSWeekCalendarUnit|NSWeekdayCalendarUnit|NSWeekdayOrdinalCalendarUnit)
fromDate:date];
NSIntegerweek=[compsweek];//今年的第几周
NSIntegerweekday=[compsweekday];//星期几(注意,周日是“1”,周一是“2”。。。。)
NSIntegerweekdayOrdinal=[compsweekdayOrdinal];//这个月的第几周
NSLog(@"week:%dweekday:%dweekdayordinal:%d",week,weekday,weekdayOrdinal);
NSDateFormatter*dateFormatter=[[NSDateFormatteralloc]init];
if(dateSwitch.on)
[dateFormattersetDateFormat:@"dd-MMM-yyy,hh:mm:ss"];
else
[dateFormattersetDateFormat:@"hh:mm:ss"];
labelTime.text=[dateFormatterstringFromDate:[NSDatedate]];
labelTime.font=[UIFontsystemFontOfSize:fontSlider.value];
[dateFormatterrelease];
教科书代码用UISwitchUISlider控制大小和显示格式