【问题标题】:How to make another array containing number of days remain如何制作另一个包含天数的数组
【发布时间】:2013-03-12 10:04:35
【问题描述】:

我有如下数组

_combinedBirthdates(
    "03/12/2013",
    "03/12/2013",
    "08/13/1990",
    "12/09/1989",
    "02/06",
    "09/08",
    "03/02/1990",
    "08/22/1989",
    "03/02",
    "05/13",
    "10/16",
    "07/08",
    "08/31/1990",
    "04/14/1992",
    "12/15/1905",
    "08/14/1989",
    "10/07/1987",
    "07/25",
    "07/17/1989",
    "03/24/1987",
    "07/28/1988",
    "01/21/1990",
    "10/13"
)

上面的NSArray中的所有元素都是NSString。

如何制作另一个包含特定日期剩余天数的数组 像这样的

_newlymadeArray(
    "125",
    "200",
    "50",
    "500",
    "125",
  and so on
)

【问题讨论】:

    标签: ios objective-c date


    【解决方案1】:

    使用这个算法:

    1. 获取当前年份
    2. 将数组中的每个日期转换为当年的日期。例如,“03/02/1990”变为“03/02/2013”​​
    3. 如果步骤 2 中的日期早于当前日期,则将其年份提前一年(即“03/02/2013”​​变为“03/02/2014”)
    4. 使用this question 中的技术,找出距离第 3 步日期的天数。它将小于一年中的天数。

    【讨论】:

      【解决方案2】:
      unsigned int unitFlags =  NSDayCalendarUnit;
      NSCalendar *currCalendar = [[NSCalendar alloc]
                                   initWithCalendarIdentifier:NSGregorianCalendar];
      
      NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
      
      [dateFormatter setDateStyle:NSDateFormatterShortStyle];
      
      NSMutableArray * _newlymadeArray = [[NSMutableArray alloc] init];
      for (NSString * str in _combinedBirthdates){
      
              NSDate * toDate = [dateFormatter dateFromString:str];
              NSDateComponents *daysInfo = [currCalendar components:unitFlags fromDate:[NSDate date]  toDate:toDate  options:0];
              int days = [daysInfo day];
              [_newlymadeArray addObject:[NSString stringWithFormat:@"%d",days]];
      
          }
      

      您需要遍历第一个数组并在 NSDate 中获取日期,并使用该信息来获取从当前日期到数组中下一个日期的天数差异。

      您必须添加必要的检查,这是未经测试的代码。

      【讨论】:

      • currCalendar 和 todate 是从哪里来的?它说未声明的符号请指导
      【解决方案3】:

      试试这个代码快照。

      NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
      [formatter setTimeZone:[NSTimeZone localTimeZone]];
      [formatter setDateFormat:@"MM/dd/yyyy"];
      NSDate *currentDate = [formatter dateFromString:@"03/12/2013"];
      NSTimeInterval srcInterval = [currentDate timeIntervalSince1970];
      
      NSArray *_combinedBirthdates = @[@"03/15/2013", @"05/15/2013"];
      NSMutableArray *_newlymadeArray = [NSMutableArray array];
      const NSInteger SECONDS_PER_DAY = 60 * 60 * 24;
      
      for (NSString *one in _combinedBirthdates) {
          NSDate *destDate = [formatter dateFromString:one];
          NSTimeInterval destInterval = [destDate timeIntervalSince1970];
          NSInteger diff = (destInterval - srcInterval) / SECONDS_PER_DAY;
          [_newlymadeArray addObject:[NSString stringWithFormat:@"%d", diff]];
      }
      

      就是这样! :)

      【讨论】:

      • NSDate *currentDate = [格式化程序 dateFromString:@"03/12/2013"];如何添加其他内容而不是 2013 年 3 月 12 日,这样它每天都可以使用?
      • 0、0、“-8247”、“-8494”、“-15775”、“-15775”、“-8411”、“-8603”、“-15775”、“-15775” ”、“-15775”、“-15775”、“-8229”、“-7637”、“-39169”、“-8611”、“-9288”、“-15775”、“-8639”、“-9485” ”、“-8993”、“-8451”、“-15775”、“-2071”、“-887”、“-8314”这是我在_newlymadearray中得到的
      猜你喜欢
      • 1970-01-01
      • 2020-02-03
      • 2019-01-29
      • 2019-09-03
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      • 2017-05-30
      • 1970-01-01
      相关资源
      最近更新 更多