簡體   English   中英

NSDateFormatter在今天而不是星期幾顯示

[英]NSDateFormatter display today instead of week day

我使用[formatter setDateFormat:@"EEEE, MMMM dd, h:mma"];

所以EEEE-為我顯示一天的名稱。 但是我想顯示今天或明天或昨天,而在其他情況下則顯示日期。

所以我想使用上面示例中所述的格式:

10月2日,星期一,下午4:33

但是如果星期一是今天,我想顯示:

今天,10月2日,下午4:33

如果星期一是昨天,則必須顯示:

昨天,10月2日,下午4:33

如果使用此代碼,則建議重復:

formatter.dateStyle = NSDateFormatterShortStyle;
formatter.timeStyle = NSDateFormatterShortStyle;
formatter.doesRelativeDateFormatting = YES;

2015年10月2日,下午4:33事實並非如此,我希望如此。

在示例中,您指向重復項: 如何使用NSDateFormatter看到“ Today”字符串 ,第二個有2個答案,例如,僅顯示當天的名稱,而不是“ TODAY”,所以在上面第一個答案中我已經描述了為什么不起作用,因此建議我的問題重復此鏈接不是一個正確的建議。 也許它可以復制另一個鏈接,但是不能復制這個鏈接。

當然,我可以解析從格式化程序手動接收的字符串以找到一天,並在需要時將其替換為“ TODAY”,但是我只是問格式化程序是否可以自動進行設置?

創建僅包含工作日的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;
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM