-(void)prepareTime{ NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *now; NSDateComponents *comps = [[NSDateComponents alloc] init]; NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit|NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; now=[NSDate date]; comps = [calendar components:unitFlags fromDate:now]; NSInteger week = [comps weekday]; NSInteger year=[comps year]; NSInteger month = [comps month]; NSInteger day = [comps day]; NSInteger hour = [comps hour]; NSInteger min = [comps minute]; NSInteger sec = [comps second]; NSString*monthStr; if (month<10) { monthStr=[NSString stringWithFormat:@"0%ld",month]; }else { monthStr=[NSString stringWithFormat:@"%ld",month]; } NSString*dayStr; if (day<10) { dayStr=[NSString stringWithFormat:@"0%ld",day]; }else { dayStr=[NSString stringWithFormat:@"%ld",day]; } week--;//這個不知怎么當(dāng)時寫的時候需要減一 NSString*weekStr; switch (week) { case 1: { weekStr=@"周一"; } break; case 2: { weekStr=@"周二"; } break; case 3: { weekStr=@"周三"; } break; case 4: { weekStr=@"周四"; } break; case 5: { weekStr=@"周五"; } break; case 6: { weekStr=@"周六"; } break; case 7: { weekStr=@"周日"; } break; default: break; } UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(0, 160,150, 40)]; label.text=[NSString stringWithFormat:@"%ld/%@/%@ %@",year,monthStr,dayStr,weekStr]; [self addSubview:label]; } 返回當(dāng)前時間,,精確到毫秒
|
|