-(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--;//这个不知怎么当时写的时候需要减一
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];
}
返回当前时间,精确到毫秒
- (NSString *)getTimeNow
{
NSString* date;
NSDateFormatter * formatter = [[NSDateFormatter alloc ] init];
//[formatter setDateFormat:@"YYYY.MM.dd.hh.mm.ss"];
[formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss:SSS"];
date = [formatter stringFromDate:[NSDate date]];
timeNow = [[NSString alloc] initWithFormat:@"%@", date];
NSLog(@"%@", timeNow);
return timeNow;
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。