zoukankan      html  css  js  c++  java
  • iOS获取系统时间

    NSString* date;
    NSDateFormatter* formatter = [[NSDateFormatteralloc]init];
           [formattersetDateFormat:@"YYYY-MM-dd%20hh:mm:ss"];
           date = [formatterstringFromDate:[NSDatedate]];

    date 显示为 2011-11-01%2012:12:12

    想实现查找几天前的时间,可以用这个方法
      NSDate* date = [[NSDate alloc] init];
            date = [date dateByAddingTimeInterval:-5*3600*24];
    这是用现在的时间,往前面减5天,得到的时间。

     

    好简单!简单的很假!

     

     

    获取连续日期:

     NSDate *date = [NSDate date];
        // 2013-04-07 11:14:45
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        // HH是24进制,hh是12进制
        formatter.dateFormat = @"MM.dd";
        
        NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
        // HH是24进制,hh是12进制
        formatter2.dateFormat = @"YYYY-MM-dd";
        
        // formatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"] autorelease];
        NSString *string = [formatter stringFromDate:date];
        NSLog(@"%@", string);
        // 返回的格林治时间
        NSMutableArray *dates = [NSMutableArray array];
    //    NSMutableArray *weekDays = [NSMutableArray array];
        NSString * registe = @"3";
        if([registe integerValue]>1)
        {
            for (int i=0; i<[registe integerValue]; i++ ) {
                NSDate *newDate = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:([date timeIntervalSinceReferenceDate] - 24*3600*i)];
                
                NSString *currentDateStr = [formatter stringFromDate:newDate];
                [dates addObject:currentDateStr];
                   NSLog(@"——————%@",dates);
                
                   }
            for (int i= [registe intValue]; i<7; i++) {
                
                 NSDate *newDate = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:( [date timeIntervalSinceReferenceDate] +24*3600 *(i- ([registe intValue]-1))*24*3600)];
    //            NSDate *newDate = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:( [date timeIntervalSinceReferenceDate] +24*3600 *(i-1))];
                
                NSString *currentDateStr = [formatter stringFromDate:newDate];
                [dates addObject:currentDateStr];
                
                NSLog(@"哈哈哈哈%@",dates);
            }
           
            
        }

  • 相关阅读:
    ES5新特性:理解 Array 中增强的 9 个 API
    ios
    Jquery异步 Deferred Object
    ES5中新增的Array方法详细说明
    Chart
    Angular常用语句
    vticker.js--垂直滚动插件
    <css系列>之css--float总结
    理解boot.img与静态分析Android/linux内核
    理解竞争条件( Race condition)漏洞
  • 原文地址:https://www.cnblogs.com/linxiu-0925/p/5086246.html
Copyright © 2011-2022 走看看