与时间一起工作没有给出正确的结果 iphone?

发布于 2024-12-13 01:58:43 字数 1534 浏览 4 评论 0原文

我想要结束时间和当前时间之间的最大时间。我尝试了这个,但没有给出正确的结果。

    NSString *strFreq = @"2";
    NSString *strET = @"5:20 PM"
    NSDate *today = [NSDate date];
    NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]autorelease];
    NSDateComponents *dateComponentsToday = [gregorian components:(NSHourCalendarUnit  | NSMinuteCalendarUnit | NSDayCalendarUnit) fromDate:today];


    int currentHour = [dateComponentsToday hour];
    int currentMinute = [dateComponentsToday minute];
    int freqAddedMinute = currentMinute + [strFreq intValue];


    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"h:mm a"]; 
    NSDate *date = [formatter dateFromString:strET];

    NSCalendar *gregorian1 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorian1 components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:date];
    NSLog(@"currentHour %d",currentHour);
    NSLog(@"freqAddedMinute %d",freqAddedMinute);
    NSLog(@"endTimeHour %d",[components hour]);
    NSLog(@"endTimeMinute %d",[components minute]);

    int endTimeHour = [dateComponentsToday hour];
    int endTimeMinute = [dateComponentsToday minute];

    // Suppose my current time is 5:10 PM then this condition must be false but it is getting true.
    if (currentHour == endTimeHour && freqAddedMinute > endTimeMinute) {
    NSLog(@"reach end time");
    }

请帮助我。提前致谢。

I want maximum time from end time and current time. I tried this but not giving proper result.

    NSString *strFreq = @"2";
    NSString *strET = @"5:20 PM"
    NSDate *today = [NSDate date];
    NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]autorelease];
    NSDateComponents *dateComponentsToday = [gregorian components:(NSHourCalendarUnit  | NSMinuteCalendarUnit | NSDayCalendarUnit) fromDate:today];


    int currentHour = [dateComponentsToday hour];
    int currentMinute = [dateComponentsToday minute];
    int freqAddedMinute = currentMinute + [strFreq intValue];


    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"h:mm a"]; 
    NSDate *date = [formatter dateFromString:strET];

    NSCalendar *gregorian1 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorian1 components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:date];
    NSLog(@"currentHour %d",currentHour);
    NSLog(@"freqAddedMinute %d",freqAddedMinute);
    NSLog(@"endTimeHour %d",[components hour]);
    NSLog(@"endTimeMinute %d",[components minute]);

    int endTimeHour = [dateComponentsToday hour];
    int endTimeMinute = [dateComponentsToday minute];

    // Suppose my current time is 5:10 PM then this condition must be false but it is getting true.
    if (currentHour == endTimeHour && freqAddedMinute > endTimeMinute) {
    NSLog(@"reach end time");
    }

Please help me for this. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冧九 2024-12-20 01:58:43

看看这个

http://developer.apple.com/library/IOS/#documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendricalCalculations.html

NSDate *startDate = ...;
NSDate *endDate = ...;

NSCalendar *gregorian = [[NSCalendar alloc]
                 initWithCalendarIdentifier:NSGregorianCalendar];

NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;

NSDateComponents *components = [gregorian components:unitFlags
                                          fromDate:startDate
                                          toDate:endDate options:0];
NSInteger months = [components month];
NSInteger days = [components day];

Check this out

http://developer.apple.com/library/IOS/#documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendricalCalculations.html

NSDate *startDate = ...;
NSDate *endDate = ...;

NSCalendar *gregorian = [[NSCalendar alloc]
                 initWithCalendarIdentifier:NSGregorianCalendar];

NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;

NSDateComponents *components = [gregorian components:unitFlags
                                          fromDate:startDate
                                          toDate:endDate options:0];
NSInteger months = [components month];
NSInteger days = [components day];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文