phone开发 获取系统时间 星期 年 月 日 时 分 秒

来源:互联网 发布:内蒙11选5遗漏数据查询 编辑:程序博客网 时间:2024/05/19 14:00

    //获取系统当前时间

    

    - (NSString*)get_system_today_time

    

    {

        

        NSDate *date = [NSDate date];

        

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

        

        [formatter setDateStyle:kCFDateFormatterMediumStyle];

        

        [formatter setTimeStyle:kCFDateFormatterShortStyle];

        

        [formatter setDateFormat:@"YYYY-MM-dd-hh-mm-ss"];

        

        NSString *string_time = [formatter stringFromDate:date];

        

        NSArray *time = [string_time componentsSeparatedByString:@"-"];

        

        

        int value_year = [[time objectAtIndex:0]intValue];

        

        int value_month = [[time objectAtIndex:1]intValue];

        

        int value_day = [[time objectAtIndex:2]intValue];

        

        int value_hour = [[time objectAtIndex:3]intValue];

        

        int value_minute = [[time objectAtIndex:4]intValue];

        

        int value_second = [[time objectAtIndex:5]intValue];

        

        [formatter setDateFormat:@"EEEE"];

        

        NSString *week_day = [formatter stringFromDate:date];

        

        [formatter release];

        

        

       NSString *system_time = [[NSStringalloc]initWithFormat:@"{\"year\":\"%d\",\"month\":\"%d\",\"day\":\"%d\",\"hour\":\"%d\",\"minute\":\"%d\",\"second\":\"%d\",\"week_day\":\"%@\"}",value_year,value_month,value_day,value_hour,value_minute,value_second,week_day];

        

        NSLog(@"\nsystem time is %@",system_time);

        

        return [system_time autorelease];

        

    }



----------------------------------------------------------------------------


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

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 五个月的狗胆小怎么办 电脑颜色不正常分辨率调不了怎么办 宝宝认人怎么办 五个月 新生儿42天听力筛查未通过怎么办 幼儿大便干燥拉不下粑粑怎么办 宝宝大便干燥拉粑粑带浓血怎么办 新生儿便秘4天了怎么办 写卷子的题目不认识怎么办? 2个月小猫尿床怎么办 安全教案鱼刺卡住了怎么办 被鱼刺卡住怎么办安全教案 小班健康教案生病了怎么办 被蜜蜂蛰了怎么办教案 走丢了怎么办大班反思 小孩吃了硬物怎么办 小孩吞了硬物怎么办 小学二年级孩子成绩不好怎么办 二年级孩子成绩不好怎么办 一年级学的不好二年级怎么办 17个月宝宝夜奶怎么办 小孩吃积食了吐怎么办 11个宝宝不会爬怎么办 5岁宝宝严重挑食怎么办 孩子吃菜口味重怎么办? 孩子挑食不吃菜不吃肉怎么办 微信新的朋友回复频繁怎么办 忘了闺蜜生日怎么办 把闺蜜生日忘了怎么办 忘了闺蜜的生日怎么办 一岁宝宝不吃菜怎么办 幼儿园教案虫子爬进耳朵怎么办 鼻子出血怎么办幼儿说课 英语记不住发音不准怎么办 幼儿园孩子拼音记不住怎么办 孩子总是记不住东西怎么办 走丢了怎么办小班教案 走丢了怎么办小班社会 数学加减个十分不清怎么办 孩子b和d分不清怎么办 小孩b和d分不清怎么办 高一的数学不会怎么办