现在的位置: 首页 > 综合 > 正文

实用代码---取得当前的年月日,当前的时分秒获得,周几和星期几获得

2018年05月12日 ⁄ 综合 ⁄ 共 1578字 ⁄ 字号 评论关闭

年月日周几分秒代码

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);

//当前的时分秒获得

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]; // 这个月的第几周

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];

教科书代码 用 UISwitch  UISlider 控制大小 和显示格式

抱歉!评论已关闭.