NSDate delay date change

扶醉桌前 提交于 2019-12-02 04:09:49

I must admit that I do not yet understand why you want to display a "wrong" weekday name, but the following code should do what you want. This is only one of various ways to achieve your task.

Convert date to "date components:"

NSCalendar *cal = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
NSDateComponents *comp = [cal components:unitFlags fromDate:[NSDate date]];

Subtract one day if necessary:

if (comp.hour < 3) {
    comp.day -= 1;
}

Convert adjusted components back to date:

NSDate *adjustedDate = [cal dateFromComponents:comp];

Your original code, now using the adjusted date:

NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"EEEE"];
NSString *today = [f stringFromDate:adjustedDate];
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!