문제

저는 5PM IST에서 정확히 현지화를 불러내는 코드 아래를 사용하고 있습니다.그러나 해고를 기록 할 때, 나는 원하는 시간을 표시하지 않습니다.어디에서나 잘못 갔는지 여부는 무엇입니까?

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    //localNotification.fireDate = myNewDate;

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    NSDate *currentDate = [NSDate date];
    NSDate *fireDate = nil;

    //[dateComponents setDay:3];  // ...or whatever day.
    [dateComponents setHour:11];
    [dateComponents setMinute:30];

    fireDate = [calendar dateByAddingComponents:dateComponents
                                          toDate:currentDate
                                         options:0];

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

    //Optionally for time zone converstions
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

    NSString *stringFromDate = [formatter stringFromDate:fireDate];


    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateFromString = [[NSDate alloc] init];
    dateFromString = [dateFormatter dateFromString:stringFromDate];

    NSLog(@"Fire date : %@",dateFromString);
    [localNotification setFireDate:dateFromString];
.

내 출력 : 화재 날짜 : 2014-07-29 16:48:20 +0000

도움이 되었습니까?

해결책

5pm을 얻지 않는 이유는 오후 5시에 시간을 설정하려고 시도하는 코드가 없습니다. 대신이 기능이 있습니다 :

[dateComponents setHour:11];
[dateComponents setMinute:30];

fireDate = [calendar dateByAddingComponents:dateComponents
                                      toDate:currentDate
                                     options:0];
.

당신을 얻는 것은 미래에 11 시간 30 분 30 분입니다. 코드를 정확히 오전 5시 30 분에 실행하지 않으면 5PM을 얻지 못할 것입니다. 그러나 코드는 어디에서나 IST를 언급하지 않으므로 해당 구역에서 5pm을 얻지 못할 것입니다.

코드 스 니펫의 끝에서 당신이하는 것을 혼란스럽게합니다. fireDate를 문자열로 변환 한 다음 해당 문자열을 바로 날짜로 변환합니다. 이것은 목적을주지 않습니다.

특정 시간대에서 5pm을 얻으려면 다음과 같은 작업을 수행해야합니다. 이렇게하면 요청 된 시간대의 다음 5PM이 내일 오후 5시 이후에 있습니다.

    NSTimeZone *zone = [NSTimeZone timeZoneWithName:@"Asia/Kolkata"];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setTimeZone:zone];

    NSInteger targetHour = 17; // 5pm

    NSDate *now = [NSDate date];
    NSInteger componentFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit;
    NSDateComponents *nowDateComponents = [calendar components:componentFlags fromDate:now];

    if (nowDateComponents.hour >= targetHour) {
        nowDateComponents.day += 1;
    }
    nowDateComponents.hour = targetHour;

    NSDate *targetDate = [calendar dateFromComponents:nowDateComponents];
.

인간이 읽을 수있는 형식 으로이 작업을 인쇄하려면 다음과 같이 할 수 있습니다. 그러나 NSDateFormatter는 사용자에게 날짜를 제시하는 데에만 필요합니다. 원하는 NSDate를 가져 오는 부분이 아닙니다.

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeZone:zone];
    [formatter setDateStyle:NSDateFormatterLongStyle];
    [formatter setTimeStyle:NSDateFormatterLongStyle];
    NSString *targetDateString = [formatter stringFromDate:targetDate];
    NSLog(@"Target date: %@", targetDateString);
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top