实用代码---取得当前的年月日,当前的时分秒获得,周几和星期几获得
年月日,?周幾,?分秒,?代碼
NSDate?*date?=?[NSDate?date];
NSCalendar?*calendar?=?[NSCalendar?currentCalendar];
NSDateComponents?*comps;
//?年月日獲得
comps?=?[calendar?components:(NSYearCalendarUnit?|?NSMonthCalendarUnit?|?NSDayCalendarUnit)?
????????????????????????????????????????fromDate:date];
NSInteger?year?=?[comps?year];
NSInteger?month?=?[comps?month];
NSInteger?day?=?[comps?day];
NSLog(@"year:?%d?month:?%d,?day:?%d",?year,?month,?day);
//當(dāng)前的時(shí)分秒獲得
comps?=?[calendar?components:(NSHourCalendarUnit?|?NSMinuteCalendarUnit?|?NSSecondCalendarUnit)
????????????????????????????????????????fromDate:date];
NSInteger?hour?=?[comps?hour];
NSInteger?minute?=?[comps?minute];
NSInteger?second?=?[comps?second];
NSLog(@"hour:?%d?minute:?%d?second:?%d",?hour,?minute,?second);
//?周幾和星期幾獲得
comps?=?[calendar?components:(NSWeekCalendarUnit?|?NSWeekdayCalendarUnit?|?NSWeekdayOrdinalCalendarUnit)
????????????????????????????????????????fromDate:date];
NSInteger?week?=?[comps?week];?//?今年的第幾周
NSInteger?weekday?=?[comps?weekday];?//?星期幾(注意,周日是“1”,周一是“2”。。。。)
NSInteger?weekdayOrdinal?=?[comps?weekdayOrdinal];?//?這個(gè)月的第幾周
NSLog(@"week:?%d?weekday:?%d?weekday?ordinal:?%d",?week,?weekday,?weekdayOrdinal);
NSDateFormatter?*dateFormatter?=?[[NSDateFormatter?alloc]init];
????????if(dateSwitch.on)
????????[dateFormatter?setDateFormat:@"dd-MMM-yyy,hh:mm:ss"];
????????else
????????[dateFormatter?setDateFormat:@"hh:mm:ss"];
????????labelTime.text?=?[dateFormatter?stringFromDate:[NSDate?date]];
????????labelTime.font?=?[UIFont?systemFontOfSize:fontSlider.value];
????????[dateFormatter?release];
教科書(shū)代碼?用?UISwitch??UISlider?控制大小?和顯示格式
總結(jié)
以上是生活随笔為你收集整理的实用代码---取得当前的年月日,当前的时分秒获得,周几和星期几获得的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ios文件系统架构图,图片解说
- 下一篇: FLASH怎么制作箭头射中靶子的动画