我如何获得即将到来的日期-目标C [英] How can i get date of a coming day - Objective C

查看:97
本文介绍了我如何获得即将到来的日期-目标C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我说upcoming monday,那么如何获取upcoming monday的日期?

If I say upcoming monday then how can I get date of upcoming monday?

比如,今天是Thu, 04-04-2013,那么我怎样才能知道即将到来的日期是wednesday?

Like, today is Thu, 04-04-2013 than how can I get what date will be on coming wednesday?

希望我能澄清你.

在这里提出了类似的问题,但我无法理解:

Similar question was asked here but I'm not able to understand this:

获取Objective-C中下一个星期六的日期?

推荐答案

NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [NSDate date];
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date];
NSInteger todayWeekday = [weekdayComponents weekday];

enum Weeks {
    SUNDAY = 1,
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY
};

NSInteger moveDays=WEDNESDAY-todayWeekday;
if (moveDays<=0) {
    moveDays+=7;
}

NSDateComponents *components = [NSDateComponents new];
components.day=moveDays;

NSCalendar *calendar=[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDate* newDate = [calendar dateByAddingComponents:components toDate:date options:0];

NSLog(@"%@",newDate);

这篇关于我如何获得即将到来的日期-目标C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆