zoukankan      html  css  js  c++  java
  • 自学iOS-获取当前时间

    NSDate *  senddate=[NSDate date];  
    
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];  
    
    [dateformatter setDateFormat:@"YYYYMMdd"];  
    
    NSString *  locationString=[dateformatter stringFromDate:senddate];    
    
    NSLog(@"locationString:%@",locationString);
    
     [dateformatter release]; //这个有问题,显示日期不对

    关大叔的写法

    01    //获取当前时间
    02    NSDate *now = [NSDate date];
    03    NSLog(@”now date is: %@”, now);
    04
    05    NSCalendar *calendar = [NSCalendar currentCalendar];
    06    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    07    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
    08     
    09    int year = [dateComponent year];
    10    int month = [dateComponent month];
    11    int day = [dateComponent day];
    12    int hour = [dateComponent hour];
    13    int minute = [dateComponent minute];
    14    int second = [dateComponent second];
    15
    16    NSLog(@”year is: %d”, year);
    17    NSLog(@”month is: %d”, month);
    18    NSLog(@”day is: %d”, day);
    19    NSLog(@”hour is: %d”, hour);
    20    NSLog(@”minute is: %d”, minute);
    21    NSLog(@”second is: %d”, second);

    获取星期几(好用的)可以获取:年、月、日、星期几

       NSDate *datet = [NSDate date];//现在时间
        NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"Sunday", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
        
        NSCalendar *calendars = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        
        NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
        
        [calendars setTimeZone: timeZone];
        
        NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
        
        NSDateComponents *theComponents = [calendars components:calendarUnit fromDate:datet];
        
        NSInteger unitFlagss = NSCalendarUnitYear |
        NSCalendarUnitMonth |
        NSCalendarUnitDay |
        NSCalendarUnitWeekday |
        NSCalendarUnitHour |
        NSCalendarUnitMinute |
        NSCalendarUnitSecond;
        
        theComponents = [calendars components:unitFlagss fromDate:datet];
        int week = [theComponents weekday];
        int year=[theComponents year];
        int month = [theComponents month];
        int day = [theComponents day];
        
        NSLog(@"%d年%d月",year,month);
        NSLog(@"%d",day);
        
        NSLog(@"xingqi%@",[weekdays objectAtIndex:theComponents.weekday]);
  • 相关阅读:
    Spring(4)——面向切面编程(AOP模块)
    Spring(3)——装配 Spring Bean 详解
    Spring(2)——Spring IoC 详解
    Spring学习(1)——快速入门
    彼得原理(The Peter Principle)
    默菲定律 [Murphy's Law]
    EXTJS 表单提交
    在java 中,数组与 List<T> 类型的相互转换
    Eclipse 工作目录被破坏,导致Eclipse 打不开
    EXTJS 密码确认与验证
  • 原文地址:https://www.cnblogs.com/zhaixing/p/5429283.html
Copyright © 2011-2022 走看看