IOS获取当前时间-成都快上网建站

IOS获取当前时间

NSDate

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都做网站、网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的南县网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

   1.NSDate对象用来表示一个具体的时间点。

   2.NSDate是一个类簇,我们使用的NSDate对象都是它的私有子类的实体。

   3.NSDate存储的是GMT时间,使用的时候会根据 当前应用 指定的 时区 进行时间上的增减,以供计算或显示。

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
       NSDate *now;
       NSDateComponents *comps = [[NSDateComponents alloc] init];
       NSInteger unitFlags =   NSYearCalendarUnit |
                               NSMonthCalendarUnit |
                               NSDayCalendarUnit |
                               NSWeekdayCalendarUnit |
                               NSHourCalendarUnit |
                               NSMinuteCalendarUnit |
                               NSSecondCalendarUnit;
       now=[NSDate date];
       comps = [calendar components:unitFlags fromDate:now];
           
       NSInteger year = [comps year];
       NSInteger month = [comps month];
       NSInteger day = [comps day];
       NSInteger hour = [comps hour];
       NSInteger min = [comps minute];
       NSInteger sec = [comps second];
       NSLog(@"year:%ld       \n   \
             month:%ld    \n       \
             day:%ld       \n      \
             hour:%ld      \n      \
             min:%ld       \n      \
             sec:%ld",
                 
             year,               
             month,               
             day,
             hour,
             min,
             sec);
           
   //组装
       NSString *stringDate = [NSString stringWithFormat:@"%ld%ld%ld%ld%ld%ld",
                               year,
                               month,
                               day,
                               hour,
                               min,
                               sec
                                   
                               ];
           
       NSLog(@"stringDate:%@", stringDate);

获取若干天前的日期:

NSTimeInterval secondsPerDay = 24 * 60 * 60;

        for (int i = 0; i < 400; i++) {

            NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:-i*secondsPerDay];

            NSLog(@"%@", date);

        }

附件:http://down.51cto.com/data/2363334

分享标题:IOS获取当前时间
文章分享:http://kswjz.com/article/joispc.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流