如何将NSDate格式化程序与日期数组一起使用? [英] How to use NSDate formatter with an array of dates?
本文介绍了如何将NSDate格式化程序与日期数组一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从当前日期开始生成一个30天的数组,并将它们放入格式正确的数组中.我目前有一个正在运行的生成器,可以从当前日期起30天并将其放入数组中,但是我不知道如何为数组使用NSDate格式化程序.我知道如何对单个项目使用格式化程序,但是如何格式化数组中的所有日期?我想要的格式是(月日).这是我的代码:
I'm trying to generate an array of 30 days from the current date and put them into an array formatted correctly. I currently have a working generater to get 30 days from the current date and put it into an array but I don't know how to use NSDate formatter for an array. I know how to use the formatter for a single item but how can I format all the dates in my array? The format that I would like is (Month Day). Here's my code:
NSMutableArray* dates = [[NSMutableArray alloc] init];
int numberOfDays=30;
NSDate *startDate=[NSDate date];
NSDate *tempDate=[startDate copy];
for (int i=0;i<numberOfDays;i++) {
NSLog(@"%@",tempDate.description);
tempDate=[tempDate dateByAddingTimeInterval:(60*60*24)];
[dates addObject:tempDate.description];
}
NSLog(@"%@",dates);
谢谢您的任何帮助.
推荐答案
像这样使用,
NSMutableArray* dates = [[NSMutableArray alloc] init];
int numberOfDays=30;
NSDate *startDate=[NSDate date];
NSDate *tempDate=[startDate copy];
for (int i=0;i<numberOfDays;i++) {
NSLog(@"%@",tempDate.description);
tempDate=[tempDate dateByAddingTimeInterval:(60*60*24)];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd"];
NSString *dateString = [dateFormat stringFromDate:tempDate];
tempDate=[dateFormat dateFromString:dateString];
[dateFormat release];
[dates addObject:tempDate.description];
}
NSLog(@"%@",dates);
这篇关于如何将NSDate格式化程序与日期数组一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文