zoukankan      html  css  js  c++  java
  • iOS开发--获取时间

    取得当前的年月日,当前的时分秒获得,周几和星期几获得
    
    
    
    NSDate*date = [NSDate date];
    
    NSCalendar*calendar = [NSCalendar currentCalendar];
    
    NSDateComponents*comps;
    
     
    
    // 年月日获得
    
    comps =[calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |NSDayCalendarUnit)
    
                                          fromDate:date];
    
    NSIntegeryear = [comps year];
    
    NSIntegermonth = [comps month];
    
    NSIntegerday = [comps day];
    
    NSLog(@"year:%d month: %d, day: %d", year, month, day);
    
     
    
    //当前的时分秒获得
    
    comps =[calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit)
    
                                          fromDate:date];
    
    NSIntegerhour = [comps hour];
    
    NSIntegerminute = [comps minute];
    
    NSIntegersecond = [comps second];
    
    NSLog(@"hour:%d minute: %d second: %d", hour, minute, second);
    
     
    
    // 周几和星期几获得
    
    comps =[calendar components:(NSWeekCalendarUnit | NSWeekdayCalendarUnit |NSWeekdayOrdinalCalendarUnit)
    
                                          fromDate:date];
    
    NSInteger week = [comps week]; // 今年的第几周
    
    NSIntegerweekday = [comps weekday]; // 星期几(注意,周日是“1”,周一是“2”。。。。)
    
    NSIntegerweekdayOrdinal = [comps weekdayOrdinal]; // 这个月的第几周
    
    NSLog(@"week:%d weekday: %d weekday ordinal: %d", week, weekday, weekdayOrdinal);
    
     
    
    NSDateFormatter*dateFormatter = [[NSDateFormatter alloc]init];
    
           if(dateSwitch.on)
    
           [dateFormattersetDateFormat:@"dd-MMM-yyy,hh:mm:ss"];
    
           else
    
           [dateFormatter setDateFormat:@"hh:mm:ss"];
    
           labelTime.text = [dateFormatter stringFromDate:[NSDatedate]];
    
           labelTime.font = [UIFontsystemFontOfSize:fontSlider.value];
    
           [dateFormatter release];
    

      

    //后台时间戳转化为yyyyMMdd

      const long long dateTimeLong =[value longLongValue]/1000;

                    

                    NSDate *dateTime = [[NSDate alloc] initWithTimeIntervalSince1970:dateTimeLong];

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

                    [formatter setTimeStyle:NSDateFormatterNoStyle];

                    [formatter setDateStyle:NSDateFormatterMediumStyle];

                    NSLocale *formatterLocal = [[NSLocale alloc] initWithLocaleIdentifier:@"en_us"];

                    [formatter setLocale:formatterLocal];

                    [formatter setDateFormat:@"yyyyMMdd"];

                    NSDate *new = [NSDate dateWithTimeInterval:0 sinceDate:dateTime];

                    NSString *dateString = [formatter stringFromDate:new];

     

     

     

  • 相关阅读:
    为什么股票一买就跌,一卖就涨?终于找到答案了!
    搜集的一些股票讲师的博客
    一位操盘手的临别赠言
    VMware网络连接 桥接、NAt、host-only模式
    我常用的网络测试工具
    linux下性能测试工具netperf使用
    vm10虚拟机安装Mac OS X10.10教程
    ACE_Svc_Handler 通信原理
    mypwd实现
    2019-2020-1 20175307 20175308 20175319 实验五 通讯协议设计
  • 原文地址:https://www.cnblogs.com/KingQiangzi/p/4274190.html
Copyright © 2011-2022 走看看