简体   繁体   中英

How to know the given date is which month's which week?

I have a requirement, if I give the specific date, such as:

  1. Type in 2016-02-01 , should return 2016-2-1 , means 2016-02-01 belongs to 2016 year's Feb's first week.

  2. Type in 2016-09-01 , should return 2016-8-5 , means 2016-09-01 belongs to 2016 year Aug's fifth week.

  3. If 2016-01-01 is not Monday, the return year should be 2015-xx .

Attention ,at there the formula mode is from Monday to Sunday is a week. Such as 2016-04 , 2016-04-04 -> 2016-04-10 is April's first week. 2016-04-01 -> 2016-04-03 is not belongs to April's weeks, it is belongs to March.

I tried using NSCalendar, but the requirement is peculiar, I have no idea deal with it, so, someone could offer some train of thought for this?
Thanks in advance.


My tried code:

In my Util.m:

+ (NSString *)getCustomMonthAndWeek:(NSString *)formatDateStr {

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];

    NSDate *date= [[NSDate alloc] init];
    date = [formatter dateFromString:formatDateStr];

    NSCalendar *calen = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSDateComponents *dateCom = [calen components:(NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitWeekOfMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date];

    NSString *ret_str = @"";

    ret_str = [[NSString alloc] initWithFormat:@"%ld-%ld-%@", (long)dateCom.year, (long)dateCom.month, dateCom.weekOfMonth];

    return ret_str;

}

In my VC:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *string = [Util getCustomMonthAndWeek:@"2016-04-02"];  // Here prints 2016-4-1, is not my requirement, you see my Attention.
    NSLog(@"%@", string);
}

Using NSCalendar set to NSDateComponents you will find given date is which month's , week . use following code

 NSDate *date=[NSDate date];

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

NSDateComponents *dateComponents = [gregorianCalendar components:(NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitWeekOfMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date];

Components gives output Calendar Year: 2017 Month: 2 Leap month: no Day: 7 Hour: 14 Minute: 55 Week of Month: 2

NSDate *someDate = // whatever
NSDate *now = [NSDate date];
NSDateComponents *thenComponents = [[NSCalendar currentCalendar] components:NSHourCalendarUnit|NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:someDate];
NSDateComponents *nowComponents = [[NSCalendar currentCalendar] components:NSHourCalendarUnit|NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:now];
if([thenComponents year] == [nowComponents year] && [thenComponents month] == [nowComponents month] && [thenComponents day] == [nowComponents day] && [thenComponents hour] == [nowComponents hour])

{}

Remove the “hour” component if you just want to check the day, or remove both that and “day” (and replace with NSWeekCalendarUnit and the -week method) to check the week.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM