美文网首页
获取时间

获取时间

作者: 梁苏珍 | 来源:发表于2017-07-06 15:15 被阅读0次

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

    相关文章

      网友评论

          本文标题:获取时间

          本文链接:https://www.haomeiwen.com/subject/kebhhxtx.html