Objective-C:找到年份和年份的日期

时间:2016-08-10 08:27:18

标签: objective-c nsdate nsdatecomponents

我正在为遗留数据对象编写GUI,其中日期保存在整数属性RewriteEngine on RewriteCond %{REQUEST_URI} !big|red|dark [NC] RewriteRule ^ - [R=404] NSDateFormatter *formatter23 = [[NSDateFormatter alloc]init]; [formatter23 setDateFormat:@"yyyy-MM-dd--HH:mm"]; NSDate* date1 = datePicker.date; [formatter23 setDateFormat:@"mm"]; if ([[formatter23 stringFromDate:date1] isEqualToString:@"00"] || [[formatter23 stringFromDate:date1] isEqualToString:@"30"]){ NSLog(@"Date is in half an hour or hour"); } else{ NSLog(@"Some other"); } 中(真实"天才"设计... )

如何将这些数据转换为年份的日期和月份?

我目前正在尝试:

day of the year

这对我来说似乎有些过于苛刻。它可能只是巧合,它可以使用几天&31; - 在NSDateComponents类引用中没有发现任何这种情况。

调试时year给了我int theYear = 2016; int theDayOfTheYear = 222; NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [[NSDateComponents alloc] init]; [components setYear:theYear]; [components setDay:theDayOfTheYear]; NSDate* theDate = [gregorian dateFromComponents:components];   - >关闭,但它应该是po theDate(!)

要执行反向操作,即将正常的DD-MM-YYYY日期转换为2016-08-08 22:00:00 +0000,请使用此代码:

2016-08-09

但我似乎无法找到相反的方法来做相反的事情......

2 个答案:

答案 0 :(得分:1)

有时需要进行测试:)

不要忘记NSDate是GMT,因此您可能希望将[[NSTimeZone localTimeZone] secondsFromGMT]秒添加到它。

似乎你有它工作:)

- (void) testDate
{
    int startYear = 2016;
    int startMonth = 1;
    int startDay = 1;

    // Create the startDate
    NSDateComponents * startDateComp = [NSDateComponents new];
    startDateComp.day = startDay;
    startDateComp.month = startMonth;
    startDateComp.year = startYear;
    NSDate * startDate = [[NSCalendar currentCalendar] dateFromComponents:startDateComp];


    for(int i=0 ; i < 365; i++)
    {
        // add i day to startDate
        NSDate * testDate = [startDate dateByAddingTimeInterval:i * 24 * 60 * 60];

        NSUInteger dayOfYear = [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit
                                                                       inUnit:NSYearCalendarUnit
                                                                      forDate:testDate];

        XCTAssert(dayOfYear == (i + 1), @"the day of the year should be i + 1");

        // Create a date using day of year
        NSDateComponents *components = [[NSDateComponents alloc] init];
        [components setYear:startYear];
        [components setDay:dayOfYear];
        NSDate* resultDate = [[NSCalendar currentCalendar] dateFromComponents:components];

        // testing result date against test date

        NSInteger test, result;

        test = [[NSCalendar currentCalendar] component:NSCalendarUnitDay fromDate:testDate];
        result = [[NSCalendar currentCalendar] component:NSCalendarUnitDay fromDate:resultDate];
        XCTAssert(test == result);

        test = [[NSCalendar currentCalendar] component:NSCalendarUnitMonth fromDate:testDate];
        result = [[NSCalendar currentCalendar] component:NSCalendarUnitMonth fromDate:resultDate];
        XCTAssert(test == result);

        test = [[NSCalendar currentCalendar] component:NSCalendarUnitYear fromDate:testDate];
        result = [[NSCalendar currentCalendar] component:NSCalendarUnitYear fromDate:resultDate];
        XCTAssert(test == result);

    }

答案 1 :(得分:0)

请试试这个。它是更大的日历单元NSCalendarUnitEra:)

NSDate *fullDate = [NSDate date];
NSCalendar *gregorian = [NSCalendar currentCalendar];
gregorian.timeZone =  [NSTimeZone systemTimeZone];
NSUInteger dayOfYear = [gregorian ordinalityOfUnit:NSCalendarUnitDay
                                            inUnit:NSCalendarUnitEra
                                           forDate:fullDate];

NSDateComponents *dateCom = [[NSDateComponents alloc] init];
dateCom.day = dayOfYear;

NSDate *expectedDate = [gregorian dateFromComponents:dateCom];
NSLog(@"%@",expectedDate);

enter image description here