我正在制作一个我需要创建每周日历的应用。为此,我将需要当周的日期和日期(Sun Sep30,Mon Oct 1,Tue Oct2等)。有人可以指导我如何实现这个目标吗?
问候
的Pankaj
答案 0 :(得分:8)
这是一个简单的函数,它提取本周所有日期的日期,从本周日开始到周六,
func formattedDaysInThisWeek() -> [String] {
// create calendar
let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)!
// today's date
let today = NSDate()
let todayComponent = calendar.components([.Day, .Month, .Year], fromDate: today)
// range of dates in this week
let thisWeekDateRange = calendar.rangeOfUnit(.Day, inUnit:.WeekOfMonth, forDate:today)
// date interval from today to beginning of week
let dayInterval = thisWeekDateRange.location - todayComponent.day
// date for beginning day of this week, ie. this week's Sunday's date
let beginningOfWeek = calendar.dateByAddingUnit(.Day, value: dayInterval, toDate: today, options: .MatchNextTime)
var formattedDays: [String] = []
for i in 0 ..< thisWeekDateRange.length {
let date = calendar.dateByAddingUnit(.Day, value: i, toDate: beginningOfWeek!, options: .MatchNextTime)!
formattedDays.append(formatDate(date))
}
return formattedDays
}
func formatDate(date: NSDate) -> String {
let format = "EEE MMMdd"
let formatter = NSDateFormatter()
formatter.dateFormat = format
return formatter.stringFromDate(date)
}
如果你调用方法 formattedDaysInThisWeek(),输出就像这样,
["Sun Oct11", "Mon Oct12", "Tue Oct13", "Wed Oct14", "Thu Oct15", "Fri Oct16", "Sat Oct17"]
答案 1 :(得分:2)
你这周需要它吗?如果是这样,这里有一些您应该可以使用的代码:
- (NSString *)stringOfDatesOfThisWeek {
NSArray *datesOfThisWeek = [self dateOfTheWeek:[NSDate date]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEE MMM d"];
NSMutableString *dateStringForThisWeek = [[NSMutableString alloc] initWithString:[formatter stringFromDate:[datesOfThisWeek firstObject]]];
for (int i = 1; i < datesOfThisWeek.count; i++) {
[dateString appendFormat:@", %@", [formatter stringFromDate:[datesOfThisWeek objectAtIndex:i]]];
}
}
- (NSArray *)datesOfTheWeek:(NSDate *)todaysDate {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger weekNumber = [[calendar components: NSWeekCalendarUnit fromDate:todaysDate] week];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comp = [gregorian components:NSYearCalendarUnit fromDate:todaysDate];
[comp setWeek:weekNumber]; //Week number.
NSMutableArray *dates = [NSMutableArray new];
for (int i = 0; i < 7; i++) {
[comp setWeekday:1]; //First day of the week. Change it to 7 to get the last date of the week
NSDate *resultDate = [gregorian dateFromComponents:comp];
[dates addObject:resultDate];
}
return dates;
}
在没有Xcode的情况下输入所有内容后,我意识到这个问题是关于Swift的。我要离开它,万一有人想要一个Objective-C答案而且偶然发现了这个问题。
答案 2 :(得分:0)
如果我理解正确,要获得预期的日期格式,您可以使用以下格式化程序NSDateFormatter
:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat:@"EEEE, MMM d";
NSLog(@"%@", [dateFormatter stringFromDate:[NSDate date]]);
您还可以设置日期格式化程序的区域设置,以使用指定语言获取工作日:
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_GB"]];
有关详细信息,请参阅Data Formatting Guide
答案 3 :(得分:0)
更新了目标 - c代码@fennelouski:
- (NSString *)stringOfDatesOfThisWeek {
NSArray *datesOfThisWeek = [self datesOfTheWeek:[NSDate date]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEE MMM d"];
NSMutableString *dateStringForThisWeek = [[NSMutableString alloc] initWithString:[formatter stringFromDate:[datesOfThisWeek firstObject]]];
for (int i = 1; i < datesOfThisWeek.count; i++) {
[dateStringForThisWeek appendFormat:@", %@", [formatter stringFromDate:[datesOfThisWeek objectAtIndex:i]]];
}
return dateStringForThisWeek;
}
- (NSArray *) datesOfTheWeek:(NSDate *)todaysDate {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger weekNumber = [[calendar components: NSCalendarUnitWeekOfMonth fromDate:todaysDate] weekOfMonth];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comp = [gregorian components:NSCalendarUnitYear fromDate:todaysDate];
[comp setWeekOfMonth:weekNumber]; //Week number.
NSMutableArray *dates = [NSMutableArray new];
for (int i = 0; i < 7; i++) {
[comp setWeekday:1]; //First day of the week. Change it to 7 to get the last date of the week
NSDate *resultDate = [gregorian dateFromComponents:comp];
[dates addObject:resultDate];
}
return dates;
}