ios开发 获取系统时间

ゝ一纸荒年。 2022-08-21 09:52 225阅读 0赞

取得当前的年月日,当前的时分秒获得,周几和星期几获得

NSDate*date = [NSDate date];

NSCalendar*calendar = [NSCalendar currentCalendar];

NSDateComponents*comps;

// 年月日获得

comps =[calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |NSDayCalendarUnit)

  1. fromDate:date\];

NSIntegeryear = [comps year];

NSIntegermonth = [comps month];

NSIntegerday = [comps day];

NSLog(@”year:%d month: %d, day: %d”, year, month, day);

//当前的时分秒获得

comps =[calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit)

  1. fromDate:date\];

NSIntegerhour = [comps hour];

NSIntegerminute = [comps minute];

NSIntegersecond = [comps second];

NSLog(@”hour:%d minute: %d second: %d”, hour, minute, second);

// 周几和星期几获得

comps =[calendar components:(NSWeekCalendarUnit | NSWeekdayCalendarUnit |NSWeekdayOrdinalCalendarUnit)

  1. fromDate:date\];

NSInteger week = [comps week]; // 今年的第几周

NSIntegerweekday = [comps weekday]; // 星期几(注意,周日是“1”,周一是“2”。。。。)

NSIntegerweekdayOrdinal = [comps weekdayOrdinal]; // 这个月的第几周

NSLog(@”week:%d weekday: %d weekday ordinal: %d”, week, weekday, weekdayOrdinal);

NSDateFormatter*dateFormatter = [[NSDateFormatter alloc]init];

  1. if(dateSwitch.on)
  2. \[dateFormattersetDateFormat:@"dd-MMM-yyy,hh:mm:ss"\];
  3. else
  4. \[dateFormatter setDateFormat:@"hh:mm:ss"\];
  5. labelTime.text = \[dateFormatter stringFromDate:\[NSDatedate\]\];
  6. labelTime.font = \[UIFontsystemFontOfSize:fontSlider.value\];
  7. \[dateFormatter release\];

发表评论

表情:
评论列表 (有 0 条评论,225人围观)

还没有评论,来说两句吧...

相关阅读