IOS获取当前时间

发布时间:2020-07-18 00:42:45 作者:江山风雨
来源:网络 阅读:594

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
推荐阅读:
  1. LR获取当前时间
  2. sqlserver 获取当前时间

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

calendar ios时间

上一篇:设计模式-简单工厂

下一篇: Fiddler教程

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》