【问题标题】:How to get dates from array of dates?如何从日期数组中获取日期?
【发布时间】:2015-03-22 16:17:23
【问题描述】:

我有一个包含不同日期的数组

    NSLog(@"Array of dates contains %@", [_dates valueForKey:@"date"]);
Array of dates contains (
    "2015-03-22 08:45:00 +0000",
    "2015-03-22 19:00:00 +0000",
    "2015-03-23 12:15:00 +0000",
    "2015-03-23 13:30:00 +0000",
    "2015-03-24 07:30:00 +0000",
    "2015-03-24 13:30:00 +0000",
    "2015-03-25 08:45:00 +0000",
    "2015-03-25 09:00:00 +0000",
    "2015-03-25 09:30:00 +0000",
    "2015-03-26 08:00:00 +0000",
    "2015-03-26 10:00:00 +0000",
    "2015-03-27 09:00:00 +0000",
    "2015-03-27 19:00:00 +0000",
    "2015-03-28 08:45:00 +0000"
)

我想从这个数组中得到另一个数组,它只包含 (2015-03-22, 2015-03-23, 2015-03-24) 请帮我解决这个问题

【问题讨论】:

标签: ios arrays date nsdateformatter


【解决方案1】:

假设您只是想要一个仅包含日期部分的新数组(删除时间信息),您可以执行以下操作

NSArray *originalArray = [_dates valueForKey:@"date"];

//Create an empty mutableArray to add the new formatted date strings into
NSMutableArray *anotherArray = [NSMutableArray alloc];

//Choosing the format of the date you require from the NSDate object
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = @"yyyy-MM-dd";

    for (NSDate *date in originalArray) {

     //This line will make a string from the date which you specified to only use the yyyy-MM-dd etc.
     NSString *datesWithoutTimeString = [formatter stringFromDate:date];

    [anotherArray addObject:datesWithoutTimeString];
    }

现在您将得到一个新的 MutableArray - anotherArray,其中包含您所需格式的日期字符串。

我希望这就是你要找的。​​p>

干杯

吉姆

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多