我使用[formatter setDateFormat:@"EEEE, MMMM dd, h:mma"];
因此EEEE - 向我展示了当天的名字。但我希望显示今天或明天或昨天,而在另一个案例中显示当天的名称。
所以我想使用上面描述的格式示例:
星期一,星期二,下午4:33
但如果星期一是今天我想表明:
今天,OCT 2,下午4:33
如果星期一是昨天,则必须显示:
昨天,OCT 2,下午4:33
如果使用此代码,则建议您重复:
formatter.dateStyle = NSDateFormatterShortStyle;
formatter.timeStyle = NSDateFormatterShortStyle;
formatter.doesRelativeDateFormatting = YES;
10/2/15,下午4:33 这不是我想要的。
在示例中,您指向重复:how to use NSDateFormatter to see "Today" string有2个答案第二个它只是显示当天的名称而不是“今天”,所以首先回答那里我已经描述了为什么它不起作用,所以说我的问题重复此链接是不正确的建议。也许它可以复制另一个链接,但不能复制这个链接。
当然我可以手动解析格式化程序中收到的字符串以找到一天并在需要时将其替换为“TODAY”,但我只是问格式化程序是否可以自动生成?
答案 0 :(得分:0)
创建仅包含工作日的NSDateFormatter将完成您想要的操作。
NSDate *now = [NSDate date];
NSDateFormatter *weekday = [[[NSDateFormatter alloc] init] autorelease];
[weekday setDateFormat: @"EEEE"];
NSLog(@"The day of the week is: %@", [weekday stringFromDate:now]);
如果您需要国际化,可以向NSDateFormatter发送一个语言环境,它将提供正确的翻译。
如果它没有帮助告诉我测试其他事情的结果
修改
-(NSString *) calculateDay {
int differenceInDays;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-DD"];
int differenceInSeconds = fabs([self timeIntervalSinceNow]);
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
// Getting components of the current date.
NSDateComponents *currentDateComponents = [currentCalendar components:NSCalendarCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSTimeZoneCalendarUnit fromDate:[NSDate date]];
// Getting components of the |date|.
NSDateComponents *dateComponents = [currentCalendar components:NSCalendarCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSTimeZoneCalendarUnit fromDate:self];
// Getting components of the difference between now and |date|.
NSDateComponents *differenceComponents = [currentCalendar components:NSDayCalendarUnit fromDate:self toDate:[NSDate date] options:0];
// NSDateComponents along with Calendar handles the timezone difference.
[currentDateComponents setTimeZone:[NSTimeZone systemTimeZone]];
[dateComponents setTimeZone:[NSTimeZone systemTimeZone]];
[differenceComponents setTimeZone:[NSTimeZone systemTimeZone]];
differenceInDays = (int)[differenceComponents day];
// This very bad if is because the components:fromDate:toDate:options: calcuates the difference in days with 24hrs difference
// So I had to handle the difference in days myself. -Mepla-
if (differenceInSeconds > [differenceComponents day]*24*3600 + [currentDateComponents hour]*3600 + [currentDateComponents minute]*60 + [currentDateComponents second]){
differenceInDays++;
}
switch (differenceInDays) {
case 0:
return @"Today";
break;
case 1:
return [BLLocalize getTextWithKey:@"Yesterday"];
break;
case 2:
case 3:
case 4: return [NSString stringWithFormat:@"%@", [[dateFormatter shortWeekdaySymbols] objectAtIndex:[dateComponents weekday]-1]]; // -1 is because the array starts at 0. -Mepla-
break;
default:{
NSString *year = [NSString stringWithFormat:@"%ld", (long)[dateComponents year]];
return [NSString stringWithFormat:@"%ld/%ld/%@", (long)[dateComponents month], (long)[dateComponents day], [year substringFromIndex: [year length] - 2]];
}
break;
}
}